采用servlet请求读流展示页面图片

来源:互联网 发布:ios软件免费下载 编辑:程序博客网 时间:2024/06/18 04:45

之前总结过 SpringMVC 的mvc:resources使用映射路径展示文件服务器上的图片  后来发现也可以采用如下的方式进行。

<img src="${rootPath}/getPicture.do?picId=${id}">

@RequestMapping(value = "/getPicture.do")public void loginCheck(HttpServletRequest request,HttpServletResponse response){String picId = request.getParameter("picId");String filePath = xxService.getPicPath(picId);File file = new File(filePath);if(file.exists()){InputStream is = null;OutputStream out = null;try{is = new BufferedInputStream(new FileInputStream(file));out = response.getOutputStream();response.setContentType("application/x-jpg");byte[] buff = new byte[1024];int length = 0;while ((length = is.read(buff)) > 0) {out.write(buff, 0, length);}out.flush();out.close();is.close();}catch(Exception e){System.out.println(e);}}}


0 0