GZIP压缩提高网络传输效率
【spring】通过GZIP压缩提高网络传输效率(可以实现任何资源的gzip压缩、包括AJAX)
gzip是http协议中使用的一种加密算法,客户端向web服务器端发出了请求后,通常情况下服务器端会将页面文件和其他资源,返回到客户端,客户端加载后渲染呈现,这种情况文件一般都比较大,如果开启Gzip ,那么服务器端响应后,会将页面,JS,CSS等文本文件或者其他文件通过高压缩算法将其压缩,然后传输到客户端,由客户端的浏览器负责解压缩与呈现。通常能节省40%以上的流量(一般都有60%左右),一些PHP,JSP文件也能够进行压缩。
1、通过WEB服务器打开GZIP压缩服务
目前大多数主流WEB中间件都支持GZIP压缩、下面以Tomcat 为例进行说明:
找到Tomcat 目录下的conf下的server.xml,并找到如下信息
将它改成如下的形式(其实在上面代码的下面已经有了,将他们打开而已。):
这样,就能够对html和xml进行压缩了,如果要压缩css 和 js,那么需要将
compressableMimeType=”text/html,text/xml”加入css和js:
一般文本类型的静态文件可以通过这种方式压缩后传输、提高传输效率。
已压缩过的静态文件(如图片)进行gzip压缩后大小基本无变化、所以一般不进行压缩。
2、通过过滤器实现gzip压缩
- 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;
- }
- chain.doFilter(request, response);
- }catch(Exception e){
- }finally{
- AcwsMonitorLog.warnHttpVisit(startTime, request);
- }
- }
- @Override
- public void destroy() {
- }
- /**
- * 判断浏览器是否支持GZIP
- * @param request
- * @return
- */
- private boolean isGZipEncoding(HttpServletRequest request){
- boolean flag=false;
- String encoding=request.getHeader("Accept-Encoding");
- if(encoding.indexOf("gzip")!=-1){
- flag=true;
- }
- return flag;
- }
- }
- package com.tyyd.framework.core;
- 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 BufferedResponse extends HttpServletResponseWrapper {
- public static final int OT_NONE = 0, OT_WRITER = 1, OT_STREAM = 2;
- private BufferedOutputStream outputStream = null;
- private PrintWriter writer = null;
- private int outputType = OT_NONE;
- public BufferedResponse(HttpServletResponse response) {
- super(response);
- outputStream = new BufferedOutputStream();
- }
- public PrintWriter getWriter() throws IOException {
- if (outputType == OT_STREAM)
- throw new IllegalStateException();
- else if (outputType == OT_WRITER)
- return writer;
- else {
- outputType = OT_WRITER;
- writer = new PrintWriter(new OutputStreamWriter(outputStream,
- getCharacterEncoding()), true);
- return writer;
- }
- }
- public ServletOutputStream getOutputStream() throws IOException {
- if (outputType == OT_WRITER)
- throw new IllegalStateException();
- else if (outputType == OT_STREAM)
- return outputStream;
- else {
- outputType = OT_STREAM;
- return outputStream;
- }
- }
- public void flushBuffer() throws IOException {
- try{writer.flush();}catch(Exception e){}
- try{outputStream.flush();}catch(Exception e){}
- }
- public void reset() {
- outputType = OT_NONE;
- outputStream.reset();
- }
- public byte[] getResponseData() throws IOException {
- flushBuffer();
- return outputStream.toByteArray();
- }
- }
- /**
- * 版权所有:
- * 项目名称:框架
- * 创建者: Wangdf
- * 创建日期: 2015-2-27
- * 文件说明: AJAX 缓存输出流
- */
- package com.tyyd.framework.core;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import javax.servlet.ServletOutputStream;
- public class BufferedOutputStream extends ServletOutputStream {
- private ByteArrayOutputStream outputStream = null;
- public BufferedOutputStream(){
- outputStream = new ByteArrayOutputStream(1024);
- }
- /**
- * Writes the specified byte to this output stream. The general
- * contract for <code>write</code> is that one byte is written
- * to the output stream. The byte to be written is the eight
- * low-order bits of the argument <code>b</code>. The 24
- * high-order bits of <code>b</code> are ignored.
- * <p>
- * Subclasses of <code>OutputStream</code> must provide an
- * implementation for this method.
- *
- * @param b the <code>byte</code>.
- * @exception IOException if an I/O error occurs. In particular,
- * an <code>IOException</code> may be thrown if the
- * output stream has been closed.
- */
- public void write(int b) throws IOException {
- outputStream.write(b);
- }
- /**
- * Writes <code>b.length</code> bytes from the specified byte array
- * to this output stream. The general contract for <code>write(b)</code>
- * is that it should have exactly the same effect as the call
- * <code>write(b, 0, b.length)</code>.
- *
- * @param b the data.
- * @exception IOException if an I/O error occurs.
- * @see java.io.OutputStream#write(byte[], int, int)
- */
- public void write(byte b[]) throws IOException {
- outputStream.write(b);
- }
- /**
- * Writes <code>len</code> bytes from the specified byte array
- * starting at offset <code>off</code> to this output stream.
- * The general contract for <code>write(b, off, len)</code> is that
- * some of the bytes in the array <code>b</code> are written to the
- * output stream in order; element <code>b[off]</code> is the first
- * byte written and <code>b[off+len-1]</code> is the last byte written
- * by this operation.
- * <p>
- * The <code>write</code> method of <code>OutputStream</code> calls
- * the write method of one argument on each of the bytes to be
- * written out. Subclasses are encouraged to override this method and
- * provide a more efficient implementation.
- * <p>
- * If <code>b</code> is <code>null</code>, a
- * <code>NullPointerException</code> is thrown.
- * <p>
- * If <code>off</code> is negative, or <code>len</code> is negative, or
- * <code>off+len</code> is greater than the length of the array
- * <code>b</code>, then an <tt>IndexOutOfBoundsException</tt> is thrown.
- *
- * @param b the data.
- * @param off the start offset in the data.
- * @param len the number of bytes to write.
- * @exception IOException if an I/O error occurs. In particular,
- * an <code>IOException</code> is thrown if the output
- * stream is closed.
- */
- public void write(byte b[], int off, int len) throws IOException {
- outputStream.write(b, off, len);
- }
- /**
- * Writes a <code>String</code> to the client,
- * without a carriage return-line feed (CRLF)
- * character at the end.
- *
- *
- * @param s the <code>String</code> to send to the client
- *
- * @exception IOException if an input or output exception occurred
- *
- */
- public void print(String s) throws IOException {
- print(s, "UTF-8");
- }
- public void print(String s, String charsetName) throws IOException {
- /*
- * 解决中文乱码问题
- */
- outputStream.write(s.getBytes(charsetName));
- }
- /**
- * Writes a <code>boolean</code> value to the client,
- * with no carriage return-line feed (CRLF)
- * character at the end.
- *
- * @param b the <code>boolean</code> value
- * to send to the client
- *
- * @exception IOException if an input or output exception occurred
- *
- */
- public void print(boolean b) throws IOException {
- print(b?"true":"false");
- }
- /**
- * Writes a character to the client,
- * with no carriage return-line feed (CRLF)
- * at the end.
- *
- * @param c the character to send to the client
- *
- * @exception IOException if an input or output exception occurred
- *
- */
- public void print(char c) throws IOException {
- print(String.valueOf(c));
- }
- /**
- *
- * Writes an int to the client,
- * with no carriage return-line feed (CRLF)
- * at the end.
- *
- * @param i the int to send to the client
- *
- * @exception IOException if an input or output exception occurred
- *
- */
- public void print(int i) throws IOException {
- print(String.valueOf(i));
- }
- /**
- *
- * Writes a <code>long</code> value to the client,
- * with no carriage return-line feed (CRLF) at the end.
- *
- * @param l the <code>long</code> value
- * to send to the client
- *
- * @exception IOException if an input or output exception
- * occurred
- *
- */
- public void print(long l) throws IOException {
- print(String.valueOf(l));
- }
- /**
- *
- * Writes a <code>float</code> value to the client,
- * with no carriage return-line feed (CRLF) at the end.
- *
- * @param f the <code>float</code> value
- * to send to the client
- *
- * @exception IOException if an input or output exception occurred
- *
- *
- */
- public void print(float f) throws IOException {
- print(String.valueOf(f));
- }
- /**
- *
- * Writes a <code>double</code> value to the client,
- * with no carriage return-line feed (CRLF) at the end.
- *
- * @param d the <code>double</code> value
- * to send to the client
- *
- * @exception IOException if an input or output exception occurred
- *
- */
- public void print(double d) throws IOException {
- print(String.valueOf(d));
- }
- /**
- * Writes a carriage return-line feed (CRLF)
- * to the client.
- *
- *
- *
- * @exception IOException if an input or output exception occurred
- *
- */
- public void println() throws IOException {
- print("\r\n");
- }
- /**
- * Writes a <code>String</code> to the client,
- * followed by a carriage return-line feed (CRLF).
- *
- *
- * @param s the <code>String</code> to write to the client
- *
- * @exception IOException if an input or output exception occurred
- *
- */
- public void println(String s){
- println(s, "UTF-8");
- }
- public void println(String s, String charsetName){
- /*
- * 解决中文乱码问题
- */
- try {
- print(s,charsetName);
- println();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- /**
- *
- * Writes a <code>boolean</code> value to the client,
- * followed by a
- * carriage return-line feed (CRLF).
- *
- *
- * @param b the <code>boolean</code> value
- * to write to the client
- *
- * @exception IOException if an input or output exception occurred
- *
- */
- public void println(boolean b) throws IOException {
- print(b);
- println();
- }
- /**
- *
- * Writes a character to the client, followed by a carriage
- * return-line feed (CRLF).
- *
- * @param c the character to write to the client
- *
- * @exception IOException if an input or output exception occurred
- *
- */
- public void println(char c) throws IOException {
- print(c);
- println();
- }
- /**
- *
- * Writes an int to the client, followed by a
- * carriage return-line feed (CRLF) character.
- *
- *
- * @param i the int to write to the client
- *
- * @exception IOException if an input or output exception occurred
- *
- */
- public void println(int i) throws IOException {
- print(i);
- println();
- }
- /**
- *
- * Writes a <code>long</code> value to the client, followed by a
- * carriage return-line feed (CRLF).
- *
- *
- * @param l the <code>long</code> value to write to the client
- *
- * @exception IOException if an input or output exception occurred
- *
- */
- public void println(long l) throws IOException {
- print(l);
- println();
- }
- /**
- *
- * Writes a <code>float</code> value to the client,
- * followed by a carriage return-line feed (CRLF).
- *
- * @param f the <code>float</code> value
- * to write to the client
- *
- *
- * @exception IOException if an input or output exception
- * occurred
- *
- */
- public void println(float f) throws IOException {
- print(f);
- println();
- }
- /**
- *
- * Writes a <code>double</code> value to the client,
- * followed by a carriage return-line feed (CRLF).
- *
- *
- * @param d the <code>double</code> value
- * to write to the client
- *
- * @exception IOException if an input or output exception occurred
- *
- */
- public void println(double d) throws IOException {
- print(d);
- println();
- }
- /**
- * Flushes this output stream and forces any buffered output bytes
- * to be written out. The general contract of <code>flush</code> is
- * that calling it is an indication that, if any bytes previously
- * written have been buffered by the implementation of the output
- * stream, such bytes should immediately be written to their
- * intended destination.
- * <p>
- * If the intended destination of this stream is an abstraction provided by
- * the underlying operating system, for example a file, then flushing the
- * stream guarantees only that bytes previously written to the stream are
- * passed to the operating system for writing; it does not guarantee that
- * they are actually written to a physical device such as a disk drive.
- * <p>
- * The <code>flush</code> method of <code>OutputStream</code> does nothing.
- *
- * @exception IOException if an I/O error occurs.
- */
- public void flush() throws IOException {
- outputStream.flush();
- }
- /**
- * Closes this output stream and releases any system resources
- * associated with this stream. The general contract of <code>close</code>
- * is that it closes the output stream. A closed stream cannot perform
- * output operations and cannot be reopened.
- * <p>
- * The <code>close</code> method of <code>OutputStream</code> does nothing.
- *
- * @exception IOException if an I/O error occurs.
- */
- public void close() throws IOException {
- outputStream.close();
- }
- /**
- * Resets the <code>count</code> field of this byte array output
- * stream to zero, so that all currently accumulated output in the
- * output stream is discarded. The output stream can be used again,
- * reusing the already allocated buffer space.
- *
- * @see java.io.ByteArrayInputStream#count
- */
- public void reset() {
- outputStream.reset();
- }
- public byte[] toByteArray() {
- return outputStream.toByteArray();
- }
- }
在web.xml中配置 PageVisitFilter,当我们访问应用中以.htm,.html,.jsp,.js,.ajax,.css结尾的资源的使用,服务器端就开启http gzip压缩,将压缩后的信息通过http 协议传递给浏览器.
- <filter>
- <filter-name>Page Visit Filter</filter-name>
- <filter-class>com.tyyd.framework.web.PageVisitFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>Page Visit Filter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
3、AJAX也可以通过这种方式压缩
只需知道ajax请求的后缀添加到下面的代码中即可:
//需要过滤的扩展名:.htm,.html,.jsp,.js,.ajax,.css
String gzippPattern=",.htm,.html,.jsp,.js,.ajax,.css,";
GZIP压缩提高网络传输效率的更多相关文章
- 网页启用Gzip压缩 提高浏览速度
启用Gzip压缩的好处 它的好处显而易见,提高网页浏览速度,无论是之前说的精简代码.压缩图片都不如启用Gzip来的实在.下图为启用Gzip后的效果. Gzip压缩效率非常高,通常可以达到70%的压缩率 ...
- 170306、wamp中的Apache开启gzip压缩提高网站的响应速度
一个网站的响应速度决定该网站的人气和质量,所以wamp配置的服务器也需要支持giz压缩来提高网站的响应速度,如何开启wamp的gzip压缩呢,经过在网站查找资料结合自己服务器中的配置,现在将这个方法分 ...
- Nginx开启Gzip压缩提高页面加载速度
本文转自http://www.veryhuo.com/a/view/51706.html,如有侵权,请及时联系转载人删除! 在实际运维中,为了提高web页面的访问加载速度,一般会把静态资源(比如js. ...
- Okhttp3请求网络开启Gzip压缩
前沿 首先OkHttp3是支持Gzip解压缩的,不过我们要明白,它是支持我们在发起请求的时候自动加入header,Accept-Encoding: gzip,而我们的服务器返回的时候header中有C ...
- 聊聊HTTP gzip压缩与常见的Android网络框架
版权声明: 欢迎转载,但请保留文章原始出处 作者:GavinCT 出处:http://www.cnblogs.com/ct2011/p/5835990.html 进入主题之前,我们先来看一下客户端与服 ...
- Nginx网络架构实战学习笔记(三):nginx gzip压缩提升网站速度、expires缓存提升网站负载、反向代理实现nginx+apache动静分离、nginx实现负载均衡
文章目录 nginx gzip压缩提升网站速度 expires缓存提升网站负载 反向代理实现nginx+apache动静分离 nginx实现负载均衡 nginx gzip压缩提升网站速度 网页内容的压 ...
- 在windows、linux中开启nginx的Gzip压缩大大提高页面、图片加载速度<转>
为了降低tomcat服务的压力,把页面上的图片采用windows版的nginx进行加载,由于有些图片比较大,加载特别的慢,所以在nginx中打开了gzip的压缩功能.加载图片的速度快了很多. 通过站长 ...
- Java Web 减少网络 IO、静态资源磁盘 IO 有效的办法--响应使用 GZIP( 压缩http请求与响应gzip压缩)
(转载http://blog.csdn.net/hylclxy/article/details/7779662) 出于节约流量考虑, 客户端在向服务端发送request的时候对post数据进行gzip ...
- nginx优化:配置gzip压缩页面提高访问速度(nginx1.18.0)
一,为什么nginx要使用gzip 1,压缩的作用: 页面使用gzip压缩之后, 页面大小可以压缩到原来的1/7左右, 传输速度和页面打开时间都可以大幅度提高, 有利于用户访问页面体验的提升 2,Ng ...
随机推荐
- tortoisegit安装
1.下载tortoisegit:https://tortoisegit.org/download/ 2.下载git 64位 3. 双击开始安装,选择默认,点击下一步 4.接着是选择安装目录,可以保持默 ...
- lfs(systemd版本)学习笔记-第2页
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! lfs(systemd)学习笔记-第1页 的地址:https://www.cnblogs.com/renren-study-no ...
- Application.Current的使用
来源 http://www.cnblogs.com/symons/archive/2010/03/15/1686200.html Application.Current的使用 WPF程序对应一个App ...
- axure工具的使用总结
---恢复内容开始--- Axure工具的使用 axure是什么? Axure RP是一款快速原型设计工具,它不需要任何编程或写代码基础,就可以设计出交互效果良好的产品原型,常用于互联网产品设计.网页 ...
- vue权威指南笔记01——样式的设置
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- The stacking context
文档中的层叠上下文由满足以下任意一个条件的元素形成: 1. z-index 值不为 "auto"的 绝对/相对定位. 2. position位fixed. 3. opacity 属 ...
- python第一百零五天 ---Django 基础 路由系统 URL 模板语言 ORM 操作
一 路由系统 URL 1 url(r'^index/',views.index) url(r'^home/', views.Home.as_view()) 2 url(r'^detail-(\d+). ...
- Spring boot+ maven + thymeleaf + HTML 实现简单的web项目
第一步: 创建一个SpringBoot应用 第二步: 创建一个实体,用来存储数据,在src/main/java/com/example/first下创建包entity , 在entity下创建Pers ...
- 12种不宜使用的javascript的语法
1. == Javascript有两组相等运算符,一组是==和!=,另一组是===和!==.前者只比较值的相等,后者除了值以外,还比较类型是否相同. 请尽量不要使用前一组,永远只使用===和!==.因 ...
- 罗马数字转整数的golang实现
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I V X L C D M 例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 写做 XII ,即为 X + ...