现在主流浏览器都是支持gzip的。服务器压缩网页后进行传输,可减少传输数据的大小使用户感觉访问速度更快。
当然,压缩也会消耗一部分服务器处理时间。 用Filter实现对返回信息的压缩
1个配置(web.xml)+3个class(核心:GZipFilter.java),不需要配置tomcat的server.xml,
配置web.xml
<filter>
<filter-name>GZipFilter</filter-name>
<filter-class>com..GZipFilter(全路径)</filter-class>
</filter>
<filter-mapping>
<filter-name>GZipFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
GZipStream.java
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.GZIPOutputStream;
import javax.servlet.ServletOutputStream;
public class GZipStream extends ServletOutputStream {
private GZIPOutputStream zipStream;
public GZipStream(OutputStream out) throws IOException {
zipStream = new GZIPOutputStream(out);
}
@Override
public void flush() throws IOException {
zipStream.flush();
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
zipStream.write(b, off, len);
}
@Override
public void write(byte[] b) throws IOException {
zipStream.write(b);
}
@Override
public void write(int arg0) throws IOException {
zipStream.write(arg0);
}
public void finish() throws IOException {
zipStream.finish();
}
public void close() throws IOException {
zipStream.close();
}
}
GZipResponse.java
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
public class GZipResponse extends HttpServletResponseWrapper {
private GZipStream stream;
private PrintWriter writer;
public GZipResponse(HttpServletResponse response) throws IOException{
super(response);
stream=new GZipStream(response.getOutputStream());
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
return stream;
}
@Override
public PrintWriter getWriter() throws IOException {
if (writer == null) {
writer = new PrintWriter(new OutputStreamWriter(
getOutputStream(), getCharacterEncoding()));
}
return writer;
}
public void flush() throws IOException {
if (writer != null) {
writer.flush();
}
stream.finish();
}
}
GZipFilter.java
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GZipFilter implements Filter {
public void destroy() {
}
public void init(FilterConfig fConfig) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest)request;
HttpServletResponse res=(HttpServletResponse)response;
if(isGZipEncoding(req)){
GZipResponse zipResponse=new GZipResponse(res);
res.setHeader("Content-Encoding", "gzip");
chain.doFilter(request, zipResponse);
zipResponse.flush();
} else {
chain.doFilter(request, response);
}
}
/**
* 判断浏览器是否支持GZIP
* @param request
* @return
*/
private static boolean isGZipEncoding(HttpServletRequest request){
boolean flag=false;
String encoding=request.getHeader("Accept-Encoding");
if(encoding.indexOf("gzip")!=-1){
flag=true;
}
return flag;
}
}
![点击并拖拽以移动 wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==](https://i-blog.csdnimg.cn/blog_migrate/8f8feb121767729627fe24c81f7d2a9e.png)
本地测试的时候要注意TTFB的时间影响
压缩方式有很多种,
reference:JSP Filter,GZIP压缩响应流,https://www.cnblogs.com/zfswff/archive/2014/01/06/3507994.html
几种压缩算法的压缩和速度比较 LZ4太快了,https://blog.csdn.net/leilonghao/article/details/73200859