当前位置: 代码迷 >> JavaScript >> struts从oracle取得图片并在jsp显示图片
  详细解决方案

struts从oracle取得图片并在jsp显示图片

热度:208   发布时间:2012-08-21 13:00:22.0
struts从oracle获得图片并在jsp显示图片

1.OutputStream write

BufferedInputStream bis = new BufferedInputStream(blob.getBinaryStream());?
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());?

InputStream in = blob.getBinaryStream();?
int size = 0;?
int b = 0;?
byte[] buffer = new byte[8192];?
response.setContentType("image/jpeg");?
response.setContentLength(in.available());?

while((b=bis.read()) != -1) {?
bos.write(b);?
}?
bos.flush();?
bos.close();?
bis.close();


2.ImageIO.write

BufferedInputStream inputimage =?new?BufferedInputStream(blob.getBinaryStream());???

BufferedImage image = null;???
try?
{???
?? image = ImageIO.read(inputimage);???
?? ServletOutputStream sos = response.getOutputStream();???
?? ImageIO.write(image, "jpg", sos);???
?? JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);???
?? encoder.encode(image);???
?? inputimage.close();???
?? sos.flush();???
}???
catch(Exception e)???
{???
?? System.out.println(e);???
}??

3.jsp

<img?src="image.do"></img>??

注:要注意思可能会有缓存问题。

  相关解决方案