(一)通过代理实现全站压缩 //通过代理来实现全站压缩过滤器 class GzipFilter2 implements Filter { public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { final HttpServletRequest request = (HttpServletRequest) req; final HttpServletResponse response = (HttpServletResponse) resp; ResponseProxy proxy = new ResponseProxy(response); chain.doFilter(request, proxy.createProxy()); //ResponseProxy.get... byte[] out = proxy.getBuffer(); //得到目标资源的输出 System.out.println(new String(out,"UTF-8")); } class ResponseProxy{ private ByteArrayOutputStream bout = new ByteArrayOutputStream(); private PrintWriter pw = null; public byte[] getBuffer(){ if(pw!=null){ pw.close(); } return bout.toByteArray(); } }
|