標籤:代碼 類型 png java 輸出 ping ges let alt
項目中Java代碼如下:
@RequestMapping("readImage")public void readImage(Model model, HttpServletRequest request, HttpServletResponse response) throws IOException{ String magazineId = request.getParameter("magazineId"); String sql = " SELECT save_path,save_name,extention FROM ta_horizon_info WHERE object_id = ‘" + magazineId + "‘ "; Map<String, Object> map = Access.getSingleMap(sql, null); String savePath = (String) map.get("save_path"); String saveName = (String) map.get("save_name"); String extention = (String) map.get("extention"); String filePath = savePath + "/" +saveName + "." + extention; File filePic = new File(filePath); if(filePic.exists()){ FileInputStream is = new FileInputStream(filePic); int i = is.available(); // 得到檔案大小 byte data[] = new byte[i]; is.read(data); // 讀資料 is.close(); response.setContentType("image/*"); // 設定返回的檔案類型 OutputStream toClient = response.getOutputStream(); // 得到向用戶端輸出位元據的對象 toClient.write(data); // 輸出資料 toClient.close(); }}
jsp頁面代碼:
<img style="width:100%; height:490px" src="<%=request.getContextPath() %>/horizon/module/magazine/readImage.wf?magazineId=${magazine.id }">
:
java 通過流的方式讀取本地圖片並顯示在jsp 頁面上