原理:讀取本地圖片,將圖片轉化成二進制流,以流的方式展現在頁面上app
/** localPath 爲圖片本地路徑 */ @RequestMapping("/imageJpeg") public String imageJpeg(HttpServletRequest request,HttpServletResponse response){ //設置驗證碼參數 response.setDateHeader("Expires", 0); response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate"); response.addHeader("Cache-Control", "post-check=0, pre-check=0"); response.setHeader("Pragma", "no-cache"); response.setContentType("image/jpeg"); try { BufferedImage image = ImageIO.read(new File(localPath)); //將內存中的圖片經過流動形式輸出到客戶端 ImageIO.write(image, "JPEG", response.getOutputStream()); } catch (Exception e) { log.error(e.getMessage(),e); } return null; }
頁面展現:post
<img src="相對應的請求url"/>