Gzip压缩优化网站
网站常使用GZIP压缩算法对网页内容进行压缩,然后传给浏览器,以减小数据传输量,提高响应速度。浏览器接收到GZIP压缩数据后会自动解压并正确显示。GZIP加速常用于解决网速慢的瓶颈。
压缩Filter中需要先判断客户浏览器时候支持GZip自动解压,如果支持,则进行GZIP压缩,否则不压缩。判断的依据是浏览器提供的Header信息,代码如下:
GZipFilter.java
- package com.rom.util;
- 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;
- /**
- * Servlet Filter implementation class GzipFilter
- */
- public class GzipFilter implements Filter {
- /**
- * Default constructor.
- */
- public GzipFilter() {
- // TODO Auto-generated constructor stub
- }
- /**
- * @see Filter#destroy()
- */
- public void destroy() {
- // TODO Auto-generated method stub
- }
- /**
- * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
- */
- public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) {
- // TODO Auto-generated method stub
- // place your code here
- System.out.println("压缩的");
- // TODO Auto-generated method stub
- HttpServletRequest request = (HttpServletRequest)req;
- HttpServletResponse response =(HttpServletResponse)res;
- String acceptEncoding =request.getHeader("Accept-Encoding");
- //支持的编码方式
- try {
- if(acceptEncoding != null && acceptEncoding.toLowerCase().indexOf("gzip") != -1){
- System.out.println("执行压缩的方法");
- //如果客户浏览器支持GZIP格式,则使用GZIP压缩数据
- GZipResponseWrapper gzipRes = new GZipResponseWrapper(response);
- chain.doFilter(request, gzipRes);//doFilter,使用自定义的response
- gzipRes.finishResponse();//输出压缩数据
- }else{
- System.out.println("没有压缩");
- chain.doFilter(request, response);//否则不压缩
- }
- // chain.doFilter(request, response);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ServletException e) {
- e.printStackTrace();
- }
- }
- /**
- * @see Filter#init(FilterConfig)
- */
- public void init(FilterConfig fConfig) throws ServletException {
- // TODO Auto-generated method stub
- }
- }
GZipResponseWrapper为自定义的response类,内部将对输出的内容进行GZIP压缩。它集成HttpServletResponseWrapper类,也是一个“伪装”的response,不真正输出内容到客户端。
GZipResponseWrapper.java
- package com.rom.util;
- 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 GZipResponseWrapper extends HttpServletResponseWrapper {
- // 默认的 response
- private HttpServletResponse response;
- // 自定义的 outputStream, 执行close()的时候对数据压缩,并输出
- private GZipOutputStream gzipOutputStream;
- // 自定义 printWriter,将内容输出到 GZipOutputStream 中
- private PrintWriter writer;
- public GZipResponseWrapper(HttpServletResponse response) throws IOException {
- super(response);
- this.response = response;
- }
- public ServletOutputStream getOutputStream() throws IOException {
- if (gzipOutputStream == null)
- gzipOutputStream = new GZipOutputStream(response);
- return gzipOutputStream;
- }
- public PrintWriter getWriter() throws IOException {
- if (writer == null)
- writer = new PrintWriter(new OutputStreamWriter(
- new GZipOutputStream(response), "UTF-8"));
- return writer;
- }
- // 压缩后数据长度会发生变化 因此将该方法内容置空
- public void setContentLength(int contentLength) {
- }
- public void flushBuffer() throws IOException {
- gzipOutputStream.flush();
- }
- public void finishResponse() throws IOException {
- if (gzipOutputStream != null)
- gzipOutputStream.close();
- if (writer != null)
- writer.close();
- }
- }
getWriter()与getOutputStream()都使用了GZipOutputStream类。这是自定义的一个ServletOutputStream类,它先将数据缓存起来,然后使用JDK自带的GZIP压缩类将数据压缩,并输出到客户端浏览器。GZIP数据压缩与输出都在该类里实现的。
GZipOutputStream.java
- package com.rom.util;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.util.zip.GZIPOutputStream;
- import javax.servlet.ServletOutputStream;
- import javax.servlet.http.HttpServletResponse;
- public class GZipOutputStream extends ServletOutputStream {
- private HttpServletResponse response;
- private GZIPOutputStream gzipOutputStream;
- private ByteArrayOutputStream byteArrayOutputStream;
- public GZipOutputStream(HttpServletResponse response) throws IOException {
- this.response = response;
- byteArrayOutputStream = new ByteArrayOutputStream();
- gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
- }
- public void write(int b) throws IOException {
- gzipOutputStream.write(b);
- }
- public void close() throws IOException {
- gzipOutputStream.finish();
- byte[] content = byteArrayOutputStream.toByteArray();
- response.addHeader("Content-Encoding", "gzip");
- response.addHeader("Content-Length", Integer.toString(content.length));
- ServletOutputStream out = response.getOutputStream();
- out.write(content);
- out.close();
- }
- public void flush() throws IOException {
- gzipOutputStream.flush();
- }
- public void write(byte[] b, int off, int len) throws IOException {
- gzipOutputStream.write(b, off, len);
- }
- public void write(byte[] b) throws IOException {
- gzipOutputStream.write(b);
- }
- }
添加xml配置文件
- <filter>
- <display-name>GzipFilter</display-name>
- <filter-name>GzipFilter</filter-name>
- <filter-class>com.rom.util.GzipFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>GzipFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
Gzip压缩优化网站的更多相关文章
- Nginx网络架构实战学习笔记(三):nginx gzip压缩提升网站速度、expires缓存提升网站负载、反向代理实现nginx+apache动静分离、nginx实现负载均衡
文章目录 nginx gzip压缩提升网站速度 expires缓存提升网站负载 反向代理实现nginx+apache动静分离 nginx实现负载均衡 nginx gzip压缩提升网站速度 网页内容的压 ...
- 13 nginx gzip压缩提升网站速度
一:nginx gzip压缩提升网站速度 我们观察news.163.com的头信息 请求: Accept-Encoding:gzip,deflate,sdch 响应: Content-Encoding ...
- IIS中启用gzip压缩(网站优化)
HTTP协议上的GZIP编码是一种用来改进WEB应用程序性能的技术.大流量的WEB站点常常使用GZIP压缩技术来让用户感受更快的速度.这一般是指WWW服务器中安装的一个功能,当有人来访问这个服务器中的 ...
- 针对ASP.NET页面实时进行GZIP压缩优化的几款压缩模块的使用简介及应用测试!(附源码)
在介绍之前,先简单说一说ASP.NET服务端GZIP压缩模块的作用及工作原理,很多人编写网页的时候页面因为使用了大量的JS特效又或者放置很多大型动态广告导致了页面或脚本体积庞大,通常都会使用一些压缩工 ...
- GZIP压缩优化
使用gzip优化web应用(filter实现) 相关知识: gzip是http协议中使用的一种加密算法,客户端向web服务器端发出了请求后,通常情况下服务器端会将页面文件和其他资源,返回到客户端,客户 ...
- Web服务器配置Gzip压缩提升网站性能
前言: HTTP协议上的GZIP编码是一种用来改进WEB应用程序性能的技术.大流量的WEB站点常常使用GZIP压缩技术来让用户感受更快的速度.这一般是指WWW服务器中安装的一个功能,当有人来访问这个服 ...
- 170306、wamp中的Apache开启gzip压缩提高网站的响应速度
一个网站的响应速度决定该网站的人气和质量,所以wamp配置的服务器也需要支持giz压缩来提高网站的响应速度,如何开启wamp的gzip压缩呢,经过在网站查找资料结合自己服务器中的配置,现在将这个方法分 ...
- nginx之gzip压缩提升网站速度
目录: 为啥使用gzip压缩 nginx使用gzip gzip的常用配置参数 nginx配置gzip 注意 为啥使用gzip压缩 开启nginx的gzip压缩,网页中的js,css等静态资源的大小会大 ...
- 2017年05月10日记一次微项目投产 | 安卓版微信内置浏览器不能解析gzip压缩过的mp4视频的问题
前言 今天投产了一个小项目,一个很简单的H5,有播放视频功能,使用了videojs插件. 之前也做过数个视频播放,视频的转压都按照既定流程进行,文件放到FTP后,iphone和安卓机测试下来都没有问题 ...
随机推荐
- 18.29SSM基础整合开发
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/d ...
- Comparable与Comparator源码分析
package java.lang; import java.util.*; /** * This interface imposes a total ordering on the objects ...
- C - Elephant(贪心)
Problem description An elephant decided to visit his friend. It turned out that the elephant's house ...
- 创建异步对象XHR的兼容写法、get、post上传数据的方式
兼容ie7以下,创建异步对象的函数 function creatXHR(){ if(typeof XMLHttpRequest != "undefined"){ return ne ...
- fragment基础 fragment生命周期 兼容低版本
fragment入门 ① 创建一个类继承Fragment 重写oncreateView方法 public class FirstFragment extends Fragment { @Overrid ...
- DevExpress 如何读取当前目录下文件,加载至grid
DBFileName=DevExpress.Utils.FileHelper.FindingFileName(Appliaction.StartupPath,"Data\\Product&g ...
- postgreSQL格式化时间的函数详解
数据类型格式化函数: PostgreSQL格式化函数提供一套有效的工具用于把各种数据类型(日期/时间.integer.floating point和numeric)转换成格式化的字符串以及反过来 ...
- VS Code中编写html(4) 标签的宽高颜色背景设置
1 <!+Tab键--> <!--有两个div标签时,分别设置style,有两种方法--> <div id="div1">第一个div标签:& ...
- HAOI2006 受欢迎的牛 缩点
不难分析出我们就是要求是否有唯一一个出度为0的强连通分量. Code: #include<cstdio> #include<stack> #include<algorit ...
- 路飞学城Python-Day141
什么是爬虫 爬虫就是通过编写程序模拟浏览器上网,然后让其去互联网上抓取数据的过程. 爬虫的目的就是为了模拟浏览器进行网络数据访问 抓取数据的两种方式 ...