hibernate blob读取图片问题

来源:互联网 发布:南极星全球通软件 编辑:程序博客网 时间:2024/05/17 22:58

我的环境是mysql+hibernate3.2.6 读取图片
1 domain:
我使用的 Blob 类型
@Lob
@Basic(fetch=javax.persistence.FetchType.EAGER)
@Column(name="content",columnDefinition="MEDIUMBLOB NOT NULL")
private Blob content;

2 action:
我可以保存,但读取到页面上时很慢
BlobService blobService = (BlobService)this.getApplicationContext().getBean("blobServiceTrans");
String contentType = "application/octet-stream;chartset=utf-8";
response.setContentType(contentType);
response.addHeader("Content-Disposition", "attachment;");
try {
OutputStream os = response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(os);
String id = request.getParameter("id");

//速度太慢
BlobIns blobIns = blobService.findBlobInsById(id);
Blob content = blobIns.getContent();
int length = (int)content.length();
byte[] temp = content.getBytes(1,length);

//InputStream is = blobIns.getContent().getBinaryStream();
//byte[] temp = new byte[1024*10];
// int i = 0;
// while((i = is.read(temp)) != -1){
// bos.write(temp,0,i);
// }
bos.write(temp);
bos.flush();
bos.close();
os.close();
//content.free();
//is.close();
} catch (IOException e) {
System.out.println("读取文件异常!");
e.printStackTrace();
}

我使用了2种办法,但都很慢,昨晚在家写的和以上的查不多但却很快就看到图片了,(图片为1.5m)
(1)直接使用Blob.getBinaryStream();得到输入流-->向输出流写内容
(2)使用Blob.getBytes(1,length)得到内容-->response的输出流里写

请教大家是什么原因?谢谢
问题补充:
谢谢huangnetian对本问题的关注,这个问题目前还没有明确的原因,后来我找公司的老同志问了问,据说可能跟容器有关如tomcat的缓存等,这2天忙着做其他的东西也没顾上,有时间好好研究下吧