public String execute()
{
***.redev.business.Categories bizCate
= new ***.redev.business.Categories();
***.redev.orm.Categories cate
= (***.redev.orm.Categories)
bizCate.Retrieve(2);
byte[] buf = cate.getPicture();
HttpServletResponse response
= org.apache.struts2.ServletActionContext.getResponse();
response.setContentType("image/jpeg");
javax.servlet.ServletOutputStream os =null;
try
{
os = response.getOutputStream();
os.write(buf);
os.close();
}
catch(java.io.IOException ex)
{
ex.printStackTrace();
}
return SUCCESS;
}
{
***.redev.business.Categories bizCate
= new ***.redev.business.Categories();
***.redev.orm.Categories cate
= (***.redev.orm.Categories)
bizCate.Retrieve(2);
byte[] buf = cate.getPicture();
HttpServletResponse response
= org.apache.struts2.ServletActionContext.getResponse();
response.setContentType("image/jpeg");
javax.servlet.ServletOutputStream os =null;
try
{
os = response.getOutputStream();
os.write(buf);
os.close();
}
catch(java.io.IOException ex)
{
ex.printStackTrace();
}
return SUCCESS;
}
一开始怎么输出图片都是那种 打红色叉号那种。 以为写法有问题,使用字符串转换为byte[]测试结果显示正常,忙活了一个早上,后来查网上有网友说:
如何取出NorthWind中Employees表中的Photo字段的图片并显示出来?
此表中图片显示常见的问题是:由于Northwind数据库內含的 image 资料最开头有78 bytes 的表头,所以需要手动将它去除。这也是大多数人费劲心思都无法显示那九个员的的图片的原因。
这不是摆明坑人,欺骗老百姓嘛!
![](https://www.cnblogs.com/Emoticons/qface/055243970.gif)
![](https://www.cnblogs.com/Emoticons/face/003.gif)
![](https://www.cnblogs.com/Emoticons/xd/009.gif)
public String execute()
{
***.redev.business.Categories bizCate
= new ***.redev.business.Categories();
***.redev.orm.Categories cate
= (***.redev.orm.Categories)
bizCate.Retrieve(2);
int offset = 78;
byte[] buf = cate.getPicture();
HttpServletResponse response
= org.apache.struts2.ServletActionContext.getResponse();
response.setContentType("image/jpeg");
javax.servlet.ServletOutputStream os =null;
try
{
os = response.getOutputStream();
os.write(buf, offset, buf.length-offset);
os.close();
}
catch(java.io.IOException ex)
{
ex.printStackTrace();
}
return SUCCESS;
}
{
***.redev.business.Categories bizCate
= new ***.redev.business.Categories();
***.redev.orm.Categories cate
= (***.redev.orm.Categories)
bizCate.Retrieve(2);
int offset = 78;
byte[] buf = cate.getPicture();
HttpServletResponse response
= org.apache.struts2.ServletActionContext.getResponse();
response.setContentType("image/jpeg");
javax.servlet.ServletOutputStream os =null;
try
{
os = response.getOutputStream();
os.write(buf, offset, buf.length-offset);
os.close();
}
catch(java.io.IOException ex)
{
ex.printStackTrace();
}
return SUCCESS;
}
果然是这个问题,数据流输出正常。