码迷,mamicode.com
首页 > 其他好文 > 详细

利用Filter实现数据的压缩回写

时间:2015-08-14 21:01:37      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:

具体的Filter实现

public class GzipFilter implements Filter {

    public void doFilter(ServletRequest req, ServletResponse resp,
            FilterChain chain) throws IOException, ServletException {
        
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        MyResponse myresponse = new MyResponse(response);
        
        
        chain.doFilter(request, myresponse);  //response.getwriter  response.getOutputStream  
        
        //取出缓冲的数据压缩后输出
        byte out[] = myresponse.getBuffer();  //得到目标资源的输出
        System.out.println("压之前:" + out.length);
        
        
        byte gzipout[] = gzip(out);
        System.out.println("压之后:" + gzipout.length);

        
        response.setHeader("content-encoding", "gzip");
        response.setHeader("content-length", gzipout.length + "");
        response.getOutputStream().write(gzipout);
    }
    
    public byte[] gzip(byte b[]) throws IOException{
        
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        GZIPOutputStream gout = new GZIPOutputStream(bout);
        gout.write(b);
        gout.close();
        return bout.toByteArray();
    }
    
    class MyResponse extends HttpServletResponseWrapper{
        private ByteArrayOutputStream bout = new ByteArrayOutputStream();
        private PrintWriter pw;
        
        private HttpServletResponse response;
        public MyResponse(HttpServletResponse response) {
            super(response);
            this.response = response;
        }
        @Override
        public ServletOutputStream getOutputStream() throws IOException {
            return new MyServletOutputStream(bout);    //myresponse.getOutputStream().write("hahah");
        }
        
        @Override
        public PrintWriter getWriter() throws IOException {
            pw = new PrintWriter(new OutputStreamWriter(bout,response.getCharacterEncoding()));
            return pw;  //MyResponse.getWriter().write("中国");
        }
        public byte[] getBuffer(){
            if(pw!=null){
                pw.close();
            }
            return bout.toByteArray();
        }
    }
    
    class MyServletOutputStream extends ServletOutputStream{

        private ByteArrayOutputStream bout;
        public MyServletOutputStream(ByteArrayOutputStream bout){
            this.bout = bout;
        }
        @Override
        public void write(int b) throws IOException {
            bout.write(b);
        }
        
    }

    public void destroy() {
        // TODO Auto-generated method stub

    }

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

    }

}

 

利用Filter实现数据的压缩回写

标签:

原文地址:http://www.cnblogs.com/zhangbaowei/p/4730922.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!