1. 程式人生 > >採用servlet請求讀流展示頁面圖片

採用servlet請求讀流展示頁面圖片

<img src="${rootPath}/getPicture.do?picId=${id}">
@RequestMapping(value = "/getPicture.do")
	public void loginCheck(HttpServletRequest request,HttpServletResponse response){

		String picId = request.getParameter("picId");
		String filePath = xxService.getPicPath(picId);
		
		File file = new File(filePath);
		if(file.exists()){
			InputStream is = null;
			OutputStream out = null;
			try{
				is = new BufferedInputStream(new FileInputStream(file));
				out = response.getOutputStream();
				response.setContentType("application/x-jpg");
				byte[] buff = new byte[1024];
				int length = 0;
				while ((length = is.read(buff)) > 0) {
					out.write(buff, 0, length);
				}
				out.flush();
				out.close();
				is.close();
			}catch(Exception e){
				System.out.println(e);
			}
		}
	}