9Filter高級開發2--實現全站壓縮

一.全站壓縮式通過包裝response來實現的。

 

二.

應用HttpServletResponseWrapper對象,壓縮響應正文內容。思路:
1 通過filter向目標頁面傳遞一個自定義的response對象;
     (1)在自定義的response對象中,重寫getOutputStream方法和getWriter方法,使目標資源調用此方法輸出頁面內容時,獲得的是我們自定義的ServletOutputStream對象。
     (2)在我們自定義的ServletOuputStream對象中,重寫write方法,使寫出的數據寫出到一個buffer中。
2.當頁面完成輸出後,在filter中就可得到頁面寫出的數據,從而我們可以調用GzipOuputStream對數據進行壓縮後再寫出給瀏覽器,以此完成響應正文件壓縮功能。
 
三、
//實現全站壓縮  /*
public class GzipFilter implements Filter {

	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
	
		HttpServletResponse response = (HttpServletResponse) resp;
		HttpServletRequest request = (HttpServletRequest) req;
		
		
		//用自己寫的response捕獲目標資源的輸出
		MyResponse myresponse = new MyResponse(response);
		chain.doFilter(request, myresponse);
		
		byte data[] = myresponse.getBufferData();  //得到目標資源輸出的數據
		System.out.println("原始數據的大小:" + data.length);
		
		gout(data,response);
		
		//byteOutputStream out;
		//servlet --->    myresponse.getOutputStream()--->MyServletOutputStream.write("hahha")
		//chain.doFilter(request, myresponse);
	}
	
	public void gout(byte data[],HttpServletResponse response) throws IOException{
		
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		GZIPOutputStream gout = new GZIPOutputStream(bout);  //buffer
		gout.write(data);
		gout.close();  //一定要有
		
		byte gzipdata[] = bout.toByteArray();
		
		response.setHeader("content-encoding", "gzip");
		response.setContentLength(gzipdata.length);
		response.getOutputStream().write(gzipdata);
		
	}
	
	class MyResponse extends HttpServletResponseWrapper{
		private HttpServletResponse response;
		private ByteArrayOutputStream bout = new ByteArrayOutputStream();
		private PrintWriter pw = null;
		
		
		public MyResponse(HttpServletResponse response) {
			super(response);
			this.response = response;
		}
		@Override
		public ServletOutputStream getOutputStream() throws IOException {
			return new MyServletOutputStream(bout);
		}
		
		@Override
		public PrintWriter getWriter() throws IOException {
			// TODO Auto-generated method stub
			pw = new PrintWriter(new OutputStreamWriter(bout,response.getCharacterEncoding()));  //write("中國")
			return pw;
		}
		public byte[] getBufferData(){
			if(pw!=null){
				pw.close();
			}
			if(bout!=null){
				return bout.toByteArray();
			}
			return null;
		}
		
	}
	
	class MyServletOutputStream extends ServletOutputStream{
		
		private ByteArrayOutputStream bout;
		public MyServletOutputStream(ByteArrayOutputStream bout){
			this.bout = bout;
		}
		
		@Override
		public void write(int b) throws IOException {  //hahah
			bout.write(b);
		}
		
	}
	
	public void destroy() {
		// TODO Auto-generated method stub

	}

	

	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub

	}

}

<filter>
  <filter-name>GzipFilter</filter-name> 
  <filter-class>cn.itcast.web.filter.example.GzipFilter</filter-class> 
</filter>
<filter-mapping>
  <filter-name>GzipFilter</filter-name> 
  <url-pattern>*.jsp</url-pattern> 
  <dispatcher>FORWARD</dispatcher> 
  <dispatcher>REQUEST</dispatcher> 
</filter-mapping>
<filter-mapping>
  <filter-name>GzipFilter</filter-name> 
  <url-pattern>*.css</url-pattern> 
</filter-mapping>
<filter-mapping>
  <filter-name>GzipFilter</filter-name> 
  <url-pattern>*.js</url-pattern> 
</filter-mapping>



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