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;
}
}
请给出具体代码
10 个解决方案
#1
#2
#4
b = rs.getBlob("photo");
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();
给你一点提示
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();
给你一点提示
#5
正好我在项目中跟你有同样的需求,给你段代码:
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();
}
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();
}
#6
老兄,如果把图片都存储在数据库中,那多浪费,为何不存储在服务器文件夹中,在数据库中只保存图片路径呢?
#7
非常感谢大家的回答,我在网上看到很多都是5楼那样的形式,可还是不知在JSP页面怎么显示,希望以后大家在回答别人的问题时能够回答得具体些,别人好给分,也好理解!
#8
详细解决办法见http://blog.csdn.net/yuyuyuyuy/archive/2011/04/02/6298753.aspx
#9
呵呵。。这就是在jsp页面中的显示op.write(buf, 0, len);通过这句在jsp页面中输出
#10
六楼的脑袋太死板了
#1
#2
#3
#4
b = rs.getBlob("photo");
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();
给你一点提示
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();
给你一点提示
#5
正好我在项目中跟你有同样的需求,给你段代码:
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();
}
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();
}
#6
老兄,如果把图片都存储在数据库中,那多浪费,为何不存储在服务器文件夹中,在数据库中只保存图片路径呢?
#7
非常感谢大家的回答,我在网上看到很多都是5楼那样的形式,可还是不知在JSP页面怎么显示,希望以后大家在回答别人的问题时能够回答得具体些,别人好给分,也好理解!
#8
详细解决办法见http://blog.csdn.net/yuyuyuyuy/archive/2011/04/02/6298753.aspx
#9
呵呵。。这就是在jsp页面中的显示op.write(buf, 0, len);通过这句在jsp页面中输出
#10
六楼的脑袋太死板了