jsp页面显示图片自然是用img标签,但是src如果直接指向图片的路径,当添加图片后去查看是显示不出来的,因为没有加载到项目中,这里提供一个将图片以二进制流的方式传递的方法。
前端代码:
src指向后台io流读取图片方法,我这里的imgId是拼接的图片名,因为我们上传时的图片名有标准规格,路径是服务器指定路径。
后台代码:
@RequestMapping("/checkImgg") public void checkImgg(@Param(“reqId”)String reqId,@Param(“imgId”)String imgId,ModelAndView mav,HttpServletResponse response,HttpServletRequest request) throws IOException{ mav.addObject(“reqId”,reqId); mav.addObject(“imgId”,imgId); String contextPath = request.getSession(). getServletContext().getRealPath("/"); //存储请求参数 //PDF字节流转为PDF显示页面 ServletOutputStream output=response.getOutputStream(); //获取返回的PDF字节流 FileInputStream in=null; File file = new File(contextPath+"/themes/default/images/reqSystem/"+reqId+"/"+imgId+".jpg"); try { in = new FileInputStream(file); byte[] bytearray = new byte[1024]; int size; while((size=in.read(bytearray))!=-1){ output.write(bytearray, 0,size); } } catch (Exception e) { e.printStackTrace(); }finally { if(in!=null){ in.close(); } output.close(); } }