后端

    @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;  
    }  

前端

方式1
<span style="white-space:pre">  </span><div style="float: left;">  
          <#--${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>  

方式二
var npcName = $('#npcImg').data('val');  
var img = document.getElementById("npcImg");  
img.src = '/userInfo/IoReadImage/'+npcName;  
方式三
</span>$('#npcImg').attr('src','/userInfo/IoReadImage/'+npcName);