java读写图片和字符串转换
? // 将字符串转换成二进制,用于显示图片???
??????? byte[] imgByte = hex2byte( imgStr );???
??????? InputStream in = new ByteArrayInputStream( imgByte );???
??????? byte[] b = new byte[1024];???
??????? int nRead = 0;???
???????????
??????? OutputStream o = response.getOutputStream();???
???????????
??????? while( ( nRead = in.read(b) ) != -1 ){???
??????????? o.write( b, 0, nRead );???
?
?
?
?
??????? }???
???????????
??????? o.flush();???
??????? o.close();???
???????????
??????? in.close();???
???????????
??? }catch(Exception e){???
??????? e.printStackTrace();???
??? }?
?
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跳转即可