解决全站压缩Filter

为提高用户体验,网页的响应速度很重要,对网页数据进行压缩输入到浏览器进行进行解压能有效的提高响应速度,可以定义一个过滤器来解决全站的压缩问题

package 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.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
//解决全站压缩过滤器
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;
         
          BufferResponse myresponse = new BufferResponse(response);
         
          chain.doFilter(request, myresponse);
         
         
          //拿出缓存中的数据,压缩后再打给浏览器
          byte out[] = myresponse.getBuffer();
          System.out.println("原始大小:" + out.length);
         
          ByteArrayOutputStream bout = new ByteArrayOutputStream();
          GZIPOutputStream gout = new GZIPOutputStream(bout);
          gout.write(out);
          gout.close();
         
         
          byte gzip[] = bout.toByteArray();
          System.out.println("压缩后的大小:" + gzip.length);
         
         
          response.setHeader("content-encoding", "gzip");
          response.setContentLength(gzip.length);
          response.getOutputStream().write(gzip);
     }
    
     public void destroy() {
     }

     public void init(FilterConfig filterConfig) throws ServletException {
     }
}

class BufferResponse extends HttpServletResponseWrapper{

     private ByteArrayOutputStream bout = new ByteArrayOutputStream();
    
     private PrintWriter pw;
     private HttpServletResponse response;
     public BufferResponse(HttpServletResponse response) {
          super(response);
          this.response = response;
     }
     @Override
     public ServletOutputStream getOutputStream() throws IOException {
          return new MyServletOutputStream(bout);
     }
     @Override
     public PrintWriter getWriter() throws IOException {
          pw = new PrintWriter(new OutputStreamWriter(bout,this.response.getCharacterEncoding())); 
          return pw;
     }
    
     public byte[] getBuffer(){
          try{
               if(pw!=null){
                    pw.close();
               }
               if(bout!=null){
                    bout.flush();
                    return bout.toByteArray();
               }
              
              
               return null;
          }catch (Exception e) {
               throw new RuntimeException(e);
          }
     }
}

class MyServletOutputStream extends ServletOutputStream{

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

然后在web.xml文件中添加过滤器配置,对特定类型的文件进行拦截压缩

<filter>
       <filter-name>GzipFilter</filter-name>
       <filter-class>web.filter.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>*.js</url-pattern>
  </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>*.html</url-pattern>
  </filter-mapping>


已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页