日期:2014-05-16  浏览次数:20294 次

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>??

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