我已经实现了将图片用二进制形式存储到mysql数据库中,并且也能从数据库中读取二进制流,将图片存储在电脑上,如何将图片流显示在jsp页面中呢?
public class MyUpAction extends ActionSupport{
private File upload = null;
···
public String getImage() throws IOException{
ImagesDAO imagesDAO = new ImagesDAOImpl();
Images img = imagesDAO.findById(5);
Blob photo = img.getImage();
try {
InputStream in = photo.getBinaryStream();
FileOutputStream fout = new FileOutputStream("e:\\t.jpg");
byte b[] = new byte[1024];
for(int i=in.read(b);i!=-1;){
fout.write(b);
in.read(b);
}
fout.flush();
fout.close();
in.close();
} catch (SQLException e) {
e.printStackTrace();
}
return SUCCESS;
}
}
请给出具体代码

解决方案 »

  1.   

    b = rs.getBlob("photo");
    long size = b.length();
    byte[] bs = b.getBytes(1, (int) size);
    response.setContentType("image/jpeg");
    response.setHeader("Cache-control", "no-cache ");
    outs = response.getOutputStream();
    outs.write(bs);
    outs.flush();给你一点提示
      

  2.   

    正好我在项目中跟你有同样的需求,给你段代码:
    while (imgs.hasNext()) {
     Img img = (Img)imgs.next();
     InputStream is = (InputStream)img.getImageio();
     ServletOutputStream op = response.getOutputStream();
     byte[] buf=new byte[1024];
     int len= is.read(buf,0,1024);
     while(len!=-1){
      op.write(buf, 0, len);
      len=is.read(buf,0,1024);
       
     }
    op.close();
    //清除输出流,防止释放时被捕获异常
            out.clear();
            out = pageContext.pushBody();
       }
      

  3.   

    非常感谢大家的回答,我在网上看到很多都是5楼那样的形式,可还是不知在JSP页面怎么显示,希望以后大家在回答别人的问题时能够回答得具体些,别人好给分,也好理解!
      

  4.   

    详细解决办法见http://blog.csdn.net/yuyuyuyuy/archive/2011/04/02/6298753.aspx
      

  5.   

    呵呵。。这就是在jsp页面中的显示op.write(buf, 0, len);通过这句在jsp页面中输出