想要在页面中读取数据库的图片 本帖最后由 wind052311 于 2014-09-25 10:30:38 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class ShowImagesServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) { String fullPath; String localPath = request.getParameter("imagePath"); String basePath = BeanHelper.getConfService().getScatter().getBasePath(); //private.xml中读取文件存放路径 fullPath = basePath + localPath; //图片的全路径 FileInputStream fis = null; OutputStream toClient = null; try { fis = new FileInputStream(fullPath); int i = fis.available(); // 得到文件大小 byte data[] = new byte[i]; fis.read(data); // 读数据 fis.close(); response.setContentType("image/*"); // 设置返回的文件类型 toClient = response.getOutputStream(); // 得到向客户端输出二进制数据的对象 toClient.write(data); // 输出数据 toClient.close(); }catch(Exception e) { e.printStackTrace(); }finally { //try中途发生异常后关闭所有IO流 closeAllIO(toClient, fis); } } //关闭所有IO流 private void closeAllIO(OutputStream toClient,FileInputStream fis) { try { if(toClient != null) toClient.close(); if(fis != null) fis.close(); }catch(IOException e) { //iLog.error("IO关闭失败:"+e); } }}这是我写的一个显示图片的后台,不过我数据库中存储的是路径,前台就是一个<img>标签,src属性是这个servlet的路径 我就是想知道前台问题,那个<img>标签,我不知道怎么插进去,想要和下面那个查看内容的写法差不多,在前台再写个类似的方法,还有后台,额,是写在Controller里吗?小弟刚入门不久,很多都不是很明白就接手这个 我就是想知道前台问题,那个<img>标签,我不知道怎么插进去,想要和下面那个查看内容的写法差不多,在前台再写个类似的方法,还有后台,额,是写在Controller里吗?小弟刚入门不久,很多都不是很明白就接手这个你用的jquery的UI么,这个我没有用过,可以给你点思路,你点击那个超链接调用一个js,用于显示一个弹出框,弹出框中引入html页面,页面是一个<img>标签,src属性由你赋值给它。后台的话就是一个servlet啊,不知道你说的Controller是什么。 img src="/picAction"/picAction 返回文件 最好的方法是吧图片存在服务器上,然后把路径存在数据库中,然后每次显示的时候直接用img的src属性写上服务器路径显示就好了。我用spring写的@RequestMapping("/upimg") @ResponseBody public String upimg( HttpServletRequest request,HttpServletResponse response){ System.out.println("asdasdas"); StringBuffer message=new StringBuffer(); String realPath = request.getSession().getServletContext().getRealPath("/upload"); MultipartHttpServletRequest fileRequest = (MultipartHttpServletRequest)request; List<MultipartFile> files = fileRequest.getFiles("myfiles"); System.out.println(files.size()); byte[] imgdata=new byte[10000000]; for (MultipartFile myfile : files) { String path = myfile.getOriginalFilename(); System.out.println("文件原名: " + path); System.out.println("文件名称: " + myfile.getName()); System.out.println("文件长度: " + myfile.getSize()); System.out.println("文件类型: " + myfile.getContentType()); System.out.println("========================================"); try { InputStream is = myfile.getInputStream(); int x=is.read(imgdata); System.out.println(x+"-->"); is.close(); response.setContentType("image/jpeg"); String apppath=request.getSession().getServletContext().getRealPath(""); //OutputStream stream = response.getOutputStream(); //stream.write(imgdata); // String apppath=request.getContextPath(); // File file=new File(apppath); File file=new File(apppath+"/img/user/img"); if (!file .exists() && !file .isDirectory()) { System.out.println("//不存在"); boolean ss=file.mkdirs(); System.out.println(ss); } else { System.out.println("//目录存在"); } message.append("img/user/img/" + path).append("|").append(path); FileOutputStream fs = new FileOutputStream(apppath+"/img/user/img/" + path); fs.write(imgdata, 0, imgdata.length); fs.flush(); fs.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return message.toString(); } 如果是已经在数据库里面存了地址的话,直接取出来用El表达式显示就可以了,<img src="${图片地址}"/> ,至于弹窗,可以用一些第三方的弹窗插件,一般就是一个div首先被隐藏,点击的时候显示并弹出,而这个图片在这个div中就可以了。 大神帮忙!!!用javabean传值的时候总是null serverlet session 的问题,神奇至极啊,CDSN无能人乎??!! ireport 在jsp中的子报表不显示 聊天室的困惑 正方形里面有个+号,-号,这样的图片到哪里下载?(在线等) jsp中汉字处理的问题!!恳请各位高手快点帮帮忙!!!急急急急!! hibernate操作关系表(中间表) servlet挂起和恢复的问题,难!!! Tomcat下的web.xml中怎么加入<listener>? oracle's long型数据的问题 mybatis延时加载无法起效 Struts2处理返回数据问题,急
public void doGet(HttpServletRequest request, HttpServletResponse response) {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) {
String fullPath;
String localPath = request.getParameter("imagePath");
String basePath = BeanHelper.getConfService().getScatter().getBasePath(); //private.xml中读取文件存放路径
fullPath = basePath + localPath; //图片的全路径
FileInputStream fis = null;
OutputStream toClient = null;
try {
fis = new FileInputStream(fullPath);
int i = fis.available(); // 得到文件大小
byte data[] = new byte[i];
fis.read(data); // 读数据
fis.close();
response.setContentType("image/*"); // 设置返回的文件类型
toClient = response.getOutputStream(); // 得到向客户端输出二进制数据的对象
toClient.write(data); // 输出数据
toClient.close();
}catch(Exception e) {
e.printStackTrace();
}finally {
//try中途发生异常后关闭所有IO流
closeAllIO(toClient, fis);
}
}
//关闭所有IO流
private void closeAllIO(OutputStream toClient,FileInputStream fis) {
try {
if(toClient != null)
toClient.close();
if(fis != null)
fis.close();
}catch(IOException e) {
//iLog.error("IO关闭失败:"+e);
}
}
}
这是我写的一个显示图片的后台,不过我数据库中存储的是路径,前台就是一个<img>标签,src属性是这个servlet的路径
你用的jquery的UI么,这个我没有用过,可以给你点思路,你点击那个超链接调用一个js,用于显示一个弹出框,弹出框中引入html页面,页面是一个<img>标签,src属性由你赋值给它。后台的话就是一个servlet啊,不知道你说的Controller是什么。
我用spring写的
@RequestMapping("/upimg")
@ResponseBody
public String upimg( HttpServletRequest request,HttpServletResponse response){
System.out.println("asdasdas");
StringBuffer message=new StringBuffer();
String realPath = request.getSession().getServletContext().getRealPath("/upload");
MultipartHttpServletRequest fileRequest = (MultipartHttpServletRequest)request;
List<MultipartFile> files = fileRequest.getFiles("myfiles");
System.out.println(files.size());
byte[] imgdata=new byte[10000000];
for (MultipartFile myfile : files)
{
String path = myfile.getOriginalFilename();
System.out.println("文件原名: " + path);
System.out.println("文件名称: " + myfile.getName());
System.out.println("文件长度: " + myfile.getSize());
System.out.println("文件类型: " + myfile.getContentType());
System.out.println("========================================");
try {
InputStream is = myfile.getInputStream();
int x=is.read(imgdata);
System.out.println(x+"-->");
is.close();
response.setContentType("image/jpeg");
String apppath=request.getSession().getServletContext().getRealPath("");
//OutputStream stream = response.getOutputStream();
//stream.write(imgdata);
// String apppath=request.getContextPath();
// File file=new File(apppath);
File file=new File(apppath+"/img/user/img");
if (!file .exists() && !file .isDirectory())
{
System.out.println("//不存在");
boolean ss=file.mkdirs(); System.out.println(ss);
} else
{
System.out.println("//目录存在");
}
message.append("img/user/img/" + path).append("|").append(path);
FileOutputStream fs = new FileOutputStream(apppath+"/img/user/img/" + path);
fs.write(imgdata, 0, imgdata.length);
fs.flush();
fs.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} return message.toString();
}