后台代码:

/**  
   * IO流读取图片 by:long  
   * @return  
   */ 
@RequestMapping(value = "/IoReadImage/{imgName}", method = RequestMethod.GET)  
  public String IoReadImage(@PathVariable String imgName,HttpServletRequest request,HttpServletResponse response) throws IOException { 
	ServletOutputStream out = null; 
	FileInputStream ips = null; 
	try { 
		//获取图片存放路径  
		String imgPath = Constans.FOLDER_IMAGE + imgName; 
		ips = new FileInputStream(new File(imgPath)); 
		response.setContentType("multipart/form-data"); 
		out = response.getOutputStream(); 
		//读取文件流  
		int len = 0; 
		byte[] buffer = new byte[1024 * 10]; 
		while ((len = ips.read(buffer)) != -1){ 
			out.write(buffer,0,len); 
		} 
		out.flush(); 
	} 
	catch (Exception e){ 
		e.printStackTrace(); 
	} 
	finally { 
		out.close(); 
		ips.close(); 
	} 
	return null; 
}

前台代码 - 方式一:

<span > </span><div >  
     <#--${model.userDatil.photo} 为数据库存放的文件名称-->  
     <img src="${ctx}/userInfo/IoReadImage/${model.userDatil.photo}" id="npcImg" width="125" height="148"/>  
     <input type="hidden" id="photo" name="photo"/>  
    </div>

js代码 - 方式二:

var npcName = $('#npcImg').data('val');  
var img = document.getElementById("npcImg");  
img.src = '/userInfo/IoReadImage/'+npcName;

jQuery代码 - 方式三:

$('#npcImg').attr('src','/userInfo/IoReadImage/'+npcName);

好了就这么简单,前台就可以显示图片了,总共才几句代码,就不额外注释说明了

java中IO流如何读取图片供前台显示


本文参考链接:https://www.yisu.com/zixun/210526.html
评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

如何解决php mail错误