1.页面中展示默认图片
<img style=" 50px;height: 50px;margin: 5px" id="imgid" src="${ctx}/sys/user/getPicById?id=${user.id}"/>
2.controller层
/* * 显示图片 */ @RequestMapping(value = {"getPicById"}) public void getPicById(User user, HttpServletRequest request, HttpServletResponse response, Model model) { try { byte[] imgData = null; byte[] data =user.getPic(); if(data!=null){ response.setContentType("image/jpeg/bng"); response.setCharacterEncoding("UTF-8"); imgData =FileUploadUtils.scaleImage( data, 100, 100); }else{ //反馈默认图片 String defaultImgPath = request.getSession().getServletContext().getRealPath("")+File.separator+"static"+File.separator+
"images"+File.separator+"default.JPG"; File defaultImgFile = new File(defaultImgPath); byte[] temp = FileUploadUtils.getBytesFromFile(defaultImgFile); imgData =FileUploadUtils.scaleImage( temp, 100, 100); } if(imgData!=null){ OutputStream out = response.getOutputStream(); InputStream in = new ByteArrayInputStream(imgData); int len=0; byte[] buf = new byte[1024]; while((len =in.read(buf,0,1024))!= -1){ out.write(buf,0,len); } out.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } model.addAttribute("user", user); } }