1.创建一个 可以使用GZIPOutputStream 压缩的流

package com.zh.yasuo2;

import java.io.IOException;
import java.util.zip.GZIPOutputStream; import javax.servlet.ServletOutputStream; public class GzipServletOupputStream extends ServletOutputStream{ private GZIPOutputStream gzipOutputStream;
@Override
public void write(int b) throws IOException {
// TODO Auto-generated method stub
System.out.println("b="+b);
gzipOutputStream.write(b);
} public GzipServletOupputStream(ServletOutputStream servletOutPutStream) throws IOException{
this.gzipOutputStream=new GZIPOutputStream(servletOutPutStream);
} public GZIPOutputStream getGZIPOutputStream(){
return gzipOutputStream;
}
} 2. 继承 HttpServletResponseWrapper 重写 getOutputStream(), getWriter()方法. 使其流是可以压缩的 package com.zh.yasuo2; import java.io.*;
import java.util.zip.GZIPOutputStream;
import javax.servlet.*;
import javax.servlet.http.*; public class GzipWrapper extends HttpServletResponseWrapper{ private GzipServletOupputStream gzipServletOupputStream;
private PrintWriter printwrite;
public GzipWrapper(HttpServletResponse response) {
super(response);
// TODO Auto-generated constructor stub
}
//重写 getOutputStream()方法
public ServletOutputStream getOutputStream()throws IOException{
if(printwrite != null){
throw new IllegalStateException();
} if(gzipServletOupputStream ==null){
gzipServletOupputStream=new GzipServletOupputStream(this.getResponse().getOutputStream());
} return gzipServletOupputStream;
} //重写getWriter()方法
public PrintWriter getWriter() throws IOException{
if(gzipServletOupputStream != null){
throw new IllegalStateException();
}
if(printwrite== null){
gzipServletOupputStream=new GzipServletOupputStream(this.getResponse().getOutputStream());
OutputStreamWriter osw=new OutputStreamWriter(gzipServletOupputStream,getResponse().getCharacterEncoding());
printwrite=new PrintWriter(osw);
//System.out.println("printwrite"+printwrite.toString());
}
return printwrite;
} public void setContentLength(int len){ } public GZIPOutputStream getGZIPOutputStream(){
if(this.gzipServletOupputStream == null){
return null;
}
return this.gzipServletOupputStream.getGZIPOutputStream();
}
} 3.使用过滤器 package com.zh.yasuo2;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
import javax.servlet.*;
import javax.servlet.http.*;
public class GzipFilter implements Filter{ public void destroy() {
// TODO Auto-generated method stub } public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest req=(HttpServletRequest) arg0;
HttpServletResponse res=(HttpServletResponse) arg1;
//用encodings 来判断浏览器是否有 gzip压缩的功能
String encodings=req.getHeader("accept-encoding"); if((encodings!=null) && (encodings.indexOf("gzip"))>-1){
GzipWrapper gzipWrapper=new GzipWrapper(res);
//设置响应内容编码为gzip格式
gzipWrapper.setHeader("content-encoding", "gzip");
arg2.doFilter(arg0, gzipWrapper);
GZIPOutputStream gzipOutputStream=gzipWrapper.getGZIPOutputStream();
if(gzipOutputStream !=null ){
gzipOutputStream.finish(); //写成压缩输出
System.out.println("finish();");
}
}else{
arg2.doFilter(arg0, arg1);
}
} public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub }
} 4.web.xml <filter>
<filter-name>GzipFilter</filter-name>
<filter-class>com.zh.yasuo2.GzipFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>GzipFilter</filter-name>
<url-pattern>/GzipServler</url-pattern>
</filter-mapping> 5. servlet 实验. package com.zh.yasuo2; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class GzipServler extends HttpServlet { /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
} }

过滤器-->GZIP压缩的更多相关文章

  1. 配gzip的过滤器进行压缩解决表单加载慢问题

    一个客户的表单上字段超过五百,经浏览器的调试器发现主要问题是从服务器取数据花费了大量时间,下载内容大小约1.2M,下载时间在10s左右,导致样式加载完大约在17s左右(不清除浏览器缓存).最终考虑利用 ...

  2. GZIP压缩优化

    使用gzip优化web应用(filter实现) 相关知识: gzip是http协议中使用的一种加密算法,客户端向web服务器端发出了请求后,通常情况下服务器端会将页面文件和其他资源,返回到客户端,客户 ...

  3. gzip压缩JavaScript

    为了提高客户端的体验效果,RIA开发逐渐兴起.这样会项目中会充斥的大量的JavaScript代码,与此同时会消耗客户端浏览器性能.对于 Ext 实现的 one page one application ...

  4. 安装mod_deflate模块启用apache的GZIP压缩

    安装mod_deflate模块启用apache的GZIP压缩 操作系统:Linux Cent OS 5 / Max OS X 10.6 snow leopard相关环境:Apache httpd 2. ...

  5. GZIP压缩提高网络传输效率

    [spring]通过GZIP压缩提高网络传输效率(可以实现任何资源的gzip压缩.包括AJAX) gzip是http协议中使用的一种加密算法,客户端向web服务器端发出了请求后,通常情况下服务器端会将 ...

  6. Asp.Net MVC 3.0 使用Gzip压缩

    前言 Gzip最早由Jean-loup Gailly和Mark Adler创建,用于Unix系统的文件压缩.我们在Linux中经常会用到后缀为.gz的文件,它们就是Gzip格式的.现今已经成为Inte ...

  7. Springboot 之 Filter 实现 Gzip 压缩超大 json 对象

    简介 在项目中,存在传递超大 json 数据的场景.直接传输超大 json 数据的话,有以下两个弊端 占用网络带宽,而有些云产品就是按照带宽来计费的,间接浪费了钱 传输数据大导致网络传输耗时较长 为了 ...

  8. ASP.NET MVC 3 网站优化总结(一) 使用 Gzip 压缩

    网站开启 Gzip 压缩的好处相信很多人都已经清楚,这样做可以提高网站的性能.那么为什么很多网站没有开启 Gzip 压缩功能呢?原因有4点:防病毒软件.浏览器 bug.网站代理和服务器未配置. 使用 ...

  9. IIS7配置Gzip压缩 JS压强失败的原因

    开启配置HTTP压缩(GZip) 在IIS7中配置Gzip压缩相比IIS6来说实在容易了许多,而且默认情况下就是启用GZip压缩的.如果没有,则可以再功能视图下找到“压缩”项,进入之后就会看到“静态内 ...

随机推荐

  1. Hbase step by step 完全分布式安装

    Step1: download and extract the packages: http://mirror.bit.edu.cn/apache/hbase/stable/ Step2: set t ...

  2. Golang tips ----- 函数

    1.在函数调用时,Golang没有默认参数值 2.一个函数声明如果没有函数体,表面该函数不是由Golang实现的,这样的声明定义了函数标识符 3.拥有函数名的函数只能在包级语法块中被声明 4.函数值( ...

  3. 【读书笔记《Android游戏编程之从零开始》】11.游戏开发基础(SurfaceView 游戏框架、View 和 SurfaceView 的区别)

    1. SurfaceView 游戏框架实例 实例效果:就是屏幕上的文本跟着点击的地方移动,效果图如下: 步骤: 新建项目“GameSurfaceView”,首先自定义一个类"MySurfac ...

  4. Windows路由表详解

    对于路由器的路由表,大部分网管朋友都很熟悉,但是对于windows的路由表,可能了解的人就相对少一些.今天我们就一起来看看windows路由表.   一. windows路由表条目解释 1. 使用ip ...

  5. 怎样用ZBrush中的Curves和Insert笔刷创建四肢

     之前的ZBrush教程给大家介绍了人体结构比例和肌肉走向,同时使用ZBrush®软件中的CuverTube笔刷为模型添加了颈部和手臂.使用InsertSphere笔刷添加腰部,本讲将继续使用Curv ...

  6. 线性代数与MATLAB2

    已知矩阵 求它们的特征值和特征向量,并绘制特征向量图,分析其几何意义 运行Meigvector.m A1=[-1 3;2 5]; A2=[1 -2;-1 5]; A3=[1 2;2 4]; A4=[2 ...

  7. Android组件系列----Android Service组件深入解析

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  8. find命令错误提示路径必须在表达式之前

    在某些版本的linux下,通过find查找当前目录下所有后缀名jpg的文件,命令为find ./ -iname *.jpg 会出现“find: 路径必须在表达式之前”的错误提示.解决的方法有两种 a. ...

  9. Android Handler处理机制 ( 二 ) ——Handler,Message,Looper,MessageQueue

    Android是消息驱动的,实现消息驱动有几个要素: 消息的表示:Message 消息队列:MessageQueue 消息循环,用于循环取出消息进行处理:Looper 消息处理,消息循环从消息队列中取 ...

  10. 12个JavaScript技巧

    转自:http://web.jobbole.com/86146/ 在这篇文章中将给大家分享12个有关于JavaScript的小技巧.这些小技巧可能在你的实际工作中或许能帮助你解决一些问题. 使用!!操 ...