spring通过GZIP压缩提高网络传输效率(可以实现任何资源的gzip压缩包括AJAX)
Posted JEECG官方博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring通过GZIP压缩提高网络传输效率(可以实现任何资源的gzip压缩包括AJAX)相关的知识,希望对你有一定的参考价值。
【spring】通过GZIP压缩提高网络传输效率(可以实现任何资源的gzip压缩、包括AJAX)
gzip是http协议中使用的一种加密算法,客户端向web服务器端发出了请求后,通常情况下服务器端会将页面文件和其他资源,返回到客户端,客户端加载后渲染呈现,这种情况文件一般都比较大,如果开启Gzip ,那么服务器端响应后,会将页面,js,CSS等文本文件或者其他文件通过高压缩算法将其压缩,然后传输到客户端,由客户端的浏览器负责解压缩与呈现。通常能节省40%以上的流量(一般都有60%左右),一些PHP,JSP文件也能够进行压缩。
1、通过WEB服务器打开GZIP压缩服务
目前大多数主流WEB中间件都支持GZIP压缩、下面以Tomcat 为例进行说明:
找到Tomcat 目录下的conf下的server.xml,并找到如下信息
<Connector port = "8080" maxHttpHeaderSize = "8192" maxThreads = "150" minSpareThreads = "25" maxSpareThreads = "75" enableLookups = "false" redirectPort = "8443" acceptCount = "100" connectionTimeout = "20000" disableUploadTimeout = "true"将它改成如下的形式(其实在上面代码的下面已经有了,将他们打开而已。):
<Connector port="8080" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" compression="on" compressionMinSize="2048" noCompressionUserAgents="gozilla, traviata" compressableMimeType="text/html,text/xml" >这样,就能够对html和xml进行压缩了,如果要压缩css 和 js,那么需要将
compressableMimeType=”text/html,text/xml”加入css和js:
<Connector port="8080" ......... compressableMimeType="text/html,text/xml,text/css,text/javascript" >一般文本类型的静态文件可以通过这种方式压缩后传输、提高传输效率。
已压缩过的静态文件(如图片)进行gzip压缩后大小基本无变化、所以一般不进行压缩。
2、通过过滤器实现gzip压缩
[java] view plain copy- package com.tyyd.framework.web;
- import java.io.IOException;
- 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 org.apache.commons.io.FilenameUtils;
- import org.apache.commons.lang.StringUtils;
- import com.tyyd.framework.core.AcwsInfo;
- import com.tyyd.framework.core.AcwsMonitorLog;
- import com.tyyd.framework.core.BufferedResponse;
- import com.tyyd.framework.core.util.ZipUtil;
- /**
- * HTTP访问过滤器
- */
- public class PageVisitFilter2 implements Filter
- @Override
- public void init(FilterConfig filterConfig) throws ServletException
- @Override
- public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException
- //性能监控
- long startTime = System.currentTimeMillis();
- HttpServletRequest request = (HttpServletRequest)req;
- HttpServletResponse response = (HttpServletResponse)res;
- String uri = request.getRequestURI();
- String ext = FilenameUtils.getExtension(uri);
- try
- response.setHeader("Pragma", "No-cache");
- response.setHeader("Cache-Control", "no-cache");
- response.setDateHeader("Expires", -1);
- request.setCharacterEncoding("UTF-8");
- response.setCharacterEncoding("UTF-8");
- response.setHeader("renderer", "webkit");
- response.setHeader("viewport", "width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0 user-scalable=no");
- if(isGZipEncoding(request))
- //需要过滤的扩展名:.htm,.html,.jsp,.js,.ajax,.css
- String gzippPattern=",.htm,.html,.jsp,.js,.ajax,.css,";
- if(StringUtils.indexOf(gzippPattern, ",."+ext+",")!=-1)
- BufferedResponse gzipResponse = new BufferedResponse(response);
- chain.doFilter(request, gzipResponse);
- byte[] srcData = gzipResponse.getResponseData();
- byte[] outData = null;
- if(srcData.length > 512)
- byte[] gzipData = ZipUtil.toGzipBytes(srcData);
- response.addHeader("Content-Encoding", "gzip");
- response.setContentLength(gzipData.length);
- outData = gzipData;
- else
- outData = srcData;
- ServletOutputStream output = response.getOutputStream();
- output.write(outData);
- output.flush();
- else
- chain.doFilter(request, response);
- return;
- Java如何在spring boot中启用gzip压缩并验证其生效