我已经实现了将图片用二进制形式存储到mysql数据库中,并且也能从数据库中读取二进制流,将图片存储在电脑上,如何将图片流显示在jsp页面中呢?
public class MyUpAction extends ActionSupport{
private File upload = null;
···
public String getImage() throws IOException{
ImagesDAO imagesDAO = new ImagesDAOImpl();
Images img = imagesDAO.findById(5);
Blob photo = img.getImage();
try {
InputStream in = photo.getBinaryStream();
FileOutputStream fout = new FileOutputStream("e:\\t.jpg");
byte b[] = new byte[1024];
for(int i=in.read(b);i!=-1;){
fout.write(b);
in.read(b);
}
fout.flush();
fout.close();
in.close();
} catch (SQLException e) {
e.printStackTrace();
}
return SUCCESS;
}
}
请给出具体代码
public class MyUpAction extends ActionSupport{
private File upload = null;
···
public String getImage() throws IOException{
ImagesDAO imagesDAO = new ImagesDAOImpl();
Images img = imagesDAO.findById(5);
Blob photo = img.getImage();
try {
InputStream in = photo.getBinaryStream();
FileOutputStream fout = new FileOutputStream("e:\\t.jpg");
byte b[] = new byte[1024];
for(int i=in.read(b);i!=-1;){
fout.write(b);
in.read(b);
}
fout.flush();
fout.close();
in.close();
} catch (SQLException e) {
e.printStackTrace();
}
return SUCCESS;
}
}
请给出具体代码
解决方案 »
- 关于CXF获取IP的方法
- 求ext中window最小化到托盘的代码
- 寻求商品分类的数据表
- 谁有 MyEclipse + ProGuard的教程??
- 新手请教,请问我用TOMCAT,如何让别人从外网浏览我的网页
- 紧急求救
- 汉字怎么样转成UTF-8码
- Jqgrid多选几行数据,但是reload刷新之后,却只有选择了一行数据
- “基于jsp+xml的学籍管理系统”中xml应该起到什么作用呢?
- JAVA项目,本机运行完全正常,部署之后表单提交的数据变成了??和小方块。。。求教
- 求救—数据库连接出错:File "/Top.jsp" not found
- java 多线程问题,解决,来个人结贴给分
long size = b.length();
byte[] bs = b.getBytes(1, (int) size);
response.setContentType("image/jpeg");
response.setHeader("Cache-control", "no-cache ");
outs = response.getOutputStream();
outs.write(bs);
outs.flush();给你一点提示
while (imgs.hasNext()) {
Img img = (Img)imgs.next();
InputStream is = (InputStream)img.getImageio();
ServletOutputStream op = response.getOutputStream();
byte[] buf=new byte[1024];
int len= is.read(buf,0,1024);
while(len!=-1){
op.write(buf, 0, len);
len=is.read(buf,0,1024);
}
op.close();
//清除输出流,防止释放时被捕获异常
out.clear();
out = pageContext.pushBody();
}