日期:2014-05-20  浏览次数:20580 次

web开发的一个问题
数据库里存储照片的字段是Blob字段,实体定义为String类型,取出该图片并在页面显示 该怎么办

------解决方案--------------------
实体定义成byte[]然后通过OutputStream 输入就好了。
------解决方案--------------------
探讨
实体定义成byte[]然后通过OutputStream 输入就好了。

------解决方案--------------------
实体定义private java.sql.Blob nr;
读取Blob image =读取数据库的blog;//附件扫描图片
if (image != null) {
try {
byte[] buf = new byte[(int)image.length()];
buf = image.getBytes(1, (int) image.length());
BufferedInputStream pi = new BufferedInputStream(image.getBinaryStream()); 
response.setContentType("image/jpeg;");
ServletOutputStream out = response.getOutputStream();
response.setContentLength(pi.available());
response.reset();
int bytesRead = 0;
while((bytesRead = pi.read(buf)) != -1) {
out.write(buf, 0, bytesRead);
}
pi.close();
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
}