IO流输出图片到前台IMG

  • A+
所属分类:Java Web

后台代码:
response.setContentType("text/html;charset=utf-8");
response.setContentType("image/jpeg");

String zp=""; //此处为图片绝对路径

FileInputStream fis = null;
OutputStream os = null;

try{
fis = new FileInputStream(zp);
os = response.getOutputStream();

int count=0;
byte[] buffer = new byte[1024*8];
while((count=fis.read(buffer))!=-1){
os.write(buffer,0,count);
}
os.flush();
}catch(Exception e){
log.debug(e.getMessage());
}finally{
if(fis!=null){
fis.close();
}
if(os!=null){
os.close();
}
}

 

 

前台接收:

<img src="这里填写后台方法的访问URL" id="photone" width="600" height="450" />

使用a标签可实现文件下载

<a href="这里填写后台方法的访问URL" >Download</a>

weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: