1. 程式人生 > >讀取本地照片 以流的形式進行顯示

讀取本地照片 以流的形式進行顯示

 獲取到前端傳來的檔名稱,到相應的檔案中去讀取,通過流的形式寫到響應體中。

/**
	 * 顯示圖片 
	 * getFeedBackPicture.do?picName=
	 * @return
	 */
	@RequestMapping(value="/viewPhoto/{photopath}")
	public void getFeedBackPicture(HttpServletResponse response,@PathVariable("photopath")String photopath) throws Exception{
		String realPath="D:/demo/download/"+photopath;
		FileInputStream inputStream = new FileInputStream(realPath);
		int i = inputStream.available();
		//byte陣列用於存放圖片位元組資料
		byte[] buff = new byte[i];
		inputStream.read(buff);
		//記得關閉輸入流
		inputStream.close();
		//設定傳送到客戶端的響應內容型別
		response.setContentType("image/*");
		OutputStream out = response.getOutputStream();
		out.write(buff);
		//關閉響應輸出流
		out.close();
	}