当前位置: 代码迷 >> Web前端 >> java读写图片跟字符串转换
  详细解决方案

java读写图片跟字符串转换

热度:440   发布时间:2013-11-20 12:46:02.0
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跳转即可

  相关解决方案