jsp頁面顯示圖片

jsp頁面顯示圖片自然是用img標籤,但是src如果直接指向圖片的路徑,當添加圖片後去查看是顯示不出來的,因爲沒有加載到項目中,這裏提供一個將圖片以二進制流的方式傳遞的方法。

前端代碼:

src指向後臺io流讀取圖片方法,我這裏的imgId是拼接的圖片名,因爲我們上傳時的圖片名有標準規格,路徑是服務器指定路徑。

<img src="<%=appPath%>/geReqAction/checkImgg.do?reqId=${reqId}&imgId=${reqId}" alt="ͼƬ1" id="jq22">

後臺代碼:

@RequestMapping("/checkImgg")
	public void checkImgg(@Param("reqId")String reqId,@Param("imgId")String imgId,ModelAndView mav,HttpServletResponse response,HttpServletRequest request) throws IOException{
		mav.addObject("reqId",reqId);
		mav.addObject("imgId",imgId);
		String contextPath = request.getSession(). getServletContext().getRealPath("/"); 
		//存儲請求參數
		//PDF字節流轉爲PDF顯示頁面
		ServletOutputStream output=response.getOutputStream();
		//獲取返回的PDF字節流
		FileInputStream in=null;
		File file = new File(contextPath+"/themes/default/images/reqSystem/"+reqId+"/"+imgId+".jpg");
		try {
			in = new FileInputStream(file);
			byte[] bytearray = new byte[1024];
			int size;
			while((size=in.read(bytearray))!=-1){
				output.write(bytearray, 0,size);
	        }
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(in!=null){
				in.close();				
			}	
			output.close();
		}
	}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章