读书人

从Oracle中读取blob部类存储的图片

发布时间: 2012-09-22 21:54:54 作者: rapoo

从Oracle中读取blob类型存储的图片
最近做个项目使用S2SH和Oracle数据库,其中photo表使用blob类型存储的图片。在网上搜了很多资料,现在把实例代码发一下。
Photo实体类


oracle中的blob类型,在实体类中使用java.sql.Blob对应。
从数据库查询数据时正常写查询方法就可以,在页面中输出图片时,在img标签的src属性写要访问的路径,我这里是PhotoAction的thumb方法。

其中contentType是下载类型,读取的图片所以${contentType}值为image/jpeg。
这样图片就从数据库中读取出来了。以上是使用struts2的方法。如果不使用struts2来实现也可以使用流输出出来。代码如下:
this.photo = this.pService.getById(this.id);response.setContentType(this.photo.getContentType());//this.photo.getContentType()值为image/jpegServletOutputStream op = response.getOutputStream();InputStream is = this.photo.getContent().getBinaryStream();BufferedInputStream bis = new BufferedInputStream(is);BufferedImage bi = ImageIO.read(bis);JPEGImageEncoderImpl jpeg = new JPEGImageEncoderImpl(op);jpeg.encode(bi);op.close();response.flushBuffer();out.clear();out = pageContext.pushBody();

读书人网 >编程

热点推荐