檔案流形式讀取圖片
阿新 • • 發佈:2019-01-04
前臺js中程式碼
if(json.data.headPhoto!=null && json.data.headPhoto!=""){ $('#headPhoto').attr('src', contextPath + '/o2oUser/getPhoto?photoURL=' + json.data.headPhoto); }else{ $('#headPhoto').attr('src', basePath + '/dist/img/SF.jpg'); }
後端讀取檔案流
/** * 獲取圖片 * @param * @return * @throws Exception */ @RequestMapping(value = "/getPhoto", method = RequestMethod.GET) @ResponseBody public void getPhoto(@RequestParam(value = "photoURL") String photoURL, HttpServletResponse response) throws Exception { logger.info("獲取圖片url:" + photoURL); InputStream inputStream = null; OutputStream os = null; try { // if(photoURL.endsWith("203_.jpg")) // photoURL = "E:/INC_SGS_CORE_PIC/20160324/201/755/59-1-715070207621595136_20160330_203_.jpg"; // else if(photoURL.endsWith("201_.jpg")) // photoURL = "E:/INC_SGS_CORE_PIC/20160324/201/755/60-1-715070207621595137_20160330_201_.jpg"; // else if(photoURL.endsWith("1667_20160324_201_755.jpg")) // photoURL = "E:/INC_SGS_CORE_PIC/20160324/201/755/30-1-712592950940401667_20160324_201_755.jpg"; // else // photoURL = "E:/INC_SGS_CORE_PIC/20160324/201/755/52-1-712839709792927745_20160324_201_755.jpg"; inputStream = new FileInputStream(new File(photoURL)); if(photoURL.endsWith(".jpg")) response.setContentType("image/jpeg; charset=GBK"); else if(photoURL.endsWith(".tif")) response.setContentType("image/tiff; charset=GBK"); os = response.getOutputStream(); byte[] b = new byte[2048]; int length; while ((length = inputStream.read(b)) > 0) { os.write(b, 0, length); } } catch (Exception e) { logger.error("獲取圖片出錯",e); e.printStackTrace(); } finally { os.close(); inputStream.close(); } }