JSP 獲取本地圖片的實例詳解
更新時間:2017年09月05日 14:55:36 作者:wjch_111
這篇文章主要介紹了JSP 獲取本地圖片的實例詳解的相關(guān)資料,希望通過本文能幫助道到大家,需要的朋友可以參考下
JSP 獲取本地圖片的實例詳解
IE當(dāng)前7以上版本不支持直接在src上寫本地硬盤地址來顯示圖片。因為我們只有通過后臺在response中讀到二進制流的方式來在前臺顯示圖片。
具體代碼如下:
public void showPicture(){ String id = ServletActionContext.getRequest().getParameter("id");//前臺傳來的存圖片路徑實體類的主鍵id HttpServletResponse response = ServletActionContext.getResponse();//struts2獲取response if(id != null && !"".equals(id)){ this.classicCases = this.classicCasesManager.findClassicCasesById(id); String pic_path = this.classicCases.getImagesLocalPath();//圖片路徑 FileInputStream is; try { is = new FileInputStream(pic_path); int i = is.available(); // 得到文件大小 byte data[] = new byte[i]; is.read(data); // 讀數(shù)據(jù) is.close(); response.setContentType("image/*"); // 設(shè)置返回的文件類型 OutputStream toClient = response.getOutputStream(); // 得到向客戶端輸出二進制數(shù)據(jù)的對象 toClient.write(data); // 輸出數(shù)據(jù) toClient.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
jsp頁面很簡單,路徑格式為,http://localhost:8080/projectName/*.action:prama=XXX
<img alt="" id="images" src="<%=basePath %>ClassicCasesAction!showPicture.action?id=${classicCases.id}">
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
request.getParameter()取值為null的解決方法
在后臺通過Request取值為null,是因為只設(shè)置了id屬性,而取值候用的是name屬性,問題就出現(xiàn)在這里2014-06-06