JSP 獲取本地圖片的實(shí)例詳解
IE當(dāng)前7以上版本不支持直接在src上寫本地硬盤地址來顯示圖片。因?yàn)槲覀冎挥型ㄟ^后臺(tái)在response中讀到二進(jìn)制流的方式來在前臺(tái)顯示圖片。
具體代碼如下:
public void showPicture(){ String id = ServletActionContext.getRequest().getParameter("id");//前臺(tái)傳來的存圖片路徑實(shí)體類的主鍵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(); // 得到向客戶端輸出二進(jìn)制數(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ū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選