採用servlet請求讀流展示頁面圖片
阿新 • • 發佈:2019-01-26
<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); } } }