您好!欢迎来天下评测网-推荐优质主机-揭露骗子垃圾主机免费收录IDC商家
首页 >> 编程语言

java压缩Gzip

package com.wordyun.web.filter;
 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.zip.GZIPOutputStream;
 
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
/**
 * GZIP压缩
 * @author Administrator
 *
 */
public class GzipFilter implements Filter {
 
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub
         
    }
 
    @Override
    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);
         
        byte[] mb=myResponse.getByte();
        System.out.println(mb.length);
        byte[] gb=getGzipByte(mb);
        System.out.println(gb.length);
        response.setHeader("content-encoding", "gzip");
        response.setHeader("content-length", gb.length+"");
         
        response.getOutputStream().write(gb);
         
    }
    private byte[] getGzipByte(byte[] mb){
         
        try {
            ByteArrayOutputStream bout=new ByteArrayOutputStream();
            GZIPOutputStream gzipout = new GZIPOutputStream(bout);
            gzipout.write(mb);
            gzipout.close();
            return bout.toByteArray();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
         
    }
    class MyResponse extends HttpServletResponseWrapper{
        private ByteArrayOutputStream bout=new ByteArrayOutputStream();
        private PrintWriter pw=null;
        private HttpServletResponse response;
        public MyResponse(HttpServletResponse response) {
            super(response);
            this.response=response;
        }
        @Override
        public ServletOutputStream getOutputStream() throws IOException {
             
            return new ServletOutputStream(){
 
                @Override
                public boolean isReady() {
                    // TODO Auto-generated method stub
                    return false;
                }
 
                @Override
                public void setWriteListener(WriteListener writeListener) {
                    // TODO Auto-generated method stub
                     
                }
 
                @Override
                public void write(int b) throws IOException {
                     
                    bout.write(b);
                }
            };
        }
         
        @Override
        public PrintWriter getWriter() throws IOException {
            pw=new PrintWriter(new OutputStreamWriter(bout, "utf-8"));
            return pw;
        }
        public byte[] getByte(){
            if(pw!=null) pw.close();
            return bout.toByteArray();
        }
         
    }
     
    @Override
    public void destroy() {
        // TODO Auto-generated method stub
         
    }
 
}

在web.xml配置过滤器

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <filter>
        <filter-name>GzipFilter</filter-name>
        <filter-class>com.wordyun.web.filter.GzipFilter</filter-class>
    </filter>
 
    <filter-mapping>
        <filter-name>GzipFilter</filter-name>
        <url-pattern>*.jsp</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>GzipFilter</filter-name>
        <url-pattern>*.html</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>GzipFilter</filter-name>
        <url-pattern>*.js</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>GzipFilter</filter-name>
        <url-pattern>*.css</url-pattern>
    </filter-mapping>
</web-app>