public String getImgFromByte() {
//業務邏輯取得圖片的byte[] 數據
.......
InputStream buffin = new ByteArrayInputStream(/*byte[]數據*/);
String str= picUrl.getImageType();
String imgtype[] = str.split("/");
BufferedImage img;
try {
img = ImageIO.read(buffin);
// 禁止圖像緩存。
this.getResponse().setHeader("Pragma", "no-cache");
this.getResponse().setHeader("Cache-Control", "no-cache");
this.getResponse().setDateHeader("Expires", 0);
this.getResponse().setContentType(picUrl.getImageType());
// 將圖像輸出到Servlet輸出流中。
ServletOutputStream sos;
sos = this.getResponse().getOutputStream();
ImageIO.write(img, imgtype[1], sos);
sos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
jsp頁面
<img src="getUserIcon!getImgFromByte.action"/></span>
struts.xml只需配置acton跳轉即可