过滤器-->GZIP压缩
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压缩的更多相关文章
- 配gzip的过滤器进行压缩解决表单加载慢问题
一个客户的表单上字段超过五百,经浏览器的调试器发现主要问题是从服务器取数据花费了大量时间,下载内容大小约1.2M,下载时间在10s左右,导致样式加载完大约在17s左右(不清除浏览器缓存).最终考虑利用 ...
- GZIP压缩优化
使用gzip优化web应用(filter实现) 相关知识: gzip是http协议中使用的一种加密算法,客户端向web服务器端发出了请求后,通常情况下服务器端会将页面文件和其他资源,返回到客户端,客户 ...
- gzip压缩JavaScript
为了提高客户端的体验效果,RIA开发逐渐兴起.这样会项目中会充斥的大量的JavaScript代码,与此同时会消耗客户端浏览器性能.对于 Ext 实现的 one page one application ...
- 安装mod_deflate模块启用apache的GZIP压缩
安装mod_deflate模块启用apache的GZIP压缩 操作系统:Linux Cent OS 5 / Max OS X 10.6 snow leopard相关环境:Apache httpd 2. ...
- GZIP压缩提高网络传输效率
[spring]通过GZIP压缩提高网络传输效率(可以实现任何资源的gzip压缩.包括AJAX) gzip是http协议中使用的一种加密算法,客户端向web服务器端发出了请求后,通常情况下服务器端会将 ...
- Asp.Net MVC 3.0 使用Gzip压缩
前言 Gzip最早由Jean-loup Gailly和Mark Adler创建,用于Unix系统的文件压缩.我们在Linux中经常会用到后缀为.gz的文件,它们就是Gzip格式的.现今已经成为Inte ...
- Springboot 之 Filter 实现 Gzip 压缩超大 json 对象
简介 在项目中,存在传递超大 json 数据的场景.直接传输超大 json 数据的话,有以下两个弊端 占用网络带宽,而有些云产品就是按照带宽来计费的,间接浪费了钱 传输数据大导致网络传输耗时较长 为了 ...
- ASP.NET MVC 3 网站优化总结(一) 使用 Gzip 压缩
网站开启 Gzip 压缩的好处相信很多人都已经清楚,这样做可以提高网站的性能.那么为什么很多网站没有开启 Gzip 压缩功能呢?原因有4点:防病毒软件.浏览器 bug.网站代理和服务器未配置. 使用 ...
- IIS7配置Gzip压缩 JS压强失败的原因
开启配置HTTP压缩(GZip) 在IIS7中配置Gzip压缩相比IIS6来说实在容易了许多,而且默认情况下就是启用GZip压缩的.如果没有,则可以再功能视图下找到“压缩”项,进入之后就会看到“静态内 ...
随机推荐
- 查看Ubuntu版本
一.查看Ubuntu版本号 方法一 root@wiki:~# cat /etc/issue Ubuntu 14.04.1 LTS \n \l 方法二 root@wiki:~# sudo lsb_rel ...
- [转]Javascript中的自执行函数表达式
[转]Javascript中的自执行函数表达式 本文转载自:http://www.ghugo.com/javascript-auto-run-function/ 以下是正文: Posted on 20 ...
- 怎么在ZBrush中通过遮罩得到子物体
ZBrush® 中通过遮罩为模型添加子物体的方法简单且方便,我们可以通过按住Ctrl键绘制遮罩结合相关命令创建具有抽出厚度的模型提取出作为子物体附在模型表面.本文将详细介绍在Zbrush中如何通过遮罩 ...
- HTML5本地存储localStorage与sessionStorage
在最近的项目中用到了html5的本地存储,下面总结一下. 1.html5几种存储形式 本地存储(localStorage && sessionStorage) 离线缓存(applica ...
- 你知道的display的值有多少?用了多少?
它的语法如下: display:none | inline | block | list-item | inline-block | table | inline-table | table-capt ...
- HDU 2899 Strange fuction 【三分】
三分可以用来求单峰函数的极值. 首先对一个函数要使用三分时,必须确保该函数在范围内是单峰的. 又因为凸函数必定是单峰的. 证明一个函数是凸函数的方法: 所以就变成证明该函数的一阶导数是否单调递增,或者 ...
- Highlighting System
Highlighting System 法线贴图漫反射着色器 Unity论坛:http://forum.unity3d.com/threads/143043-Highlighting-System-R ...
- 项目管理工具Redmine各功能测试
Redmine 是一个开源的.基于Web的项目管理和缺陷跟踪工具.它用日历和甘特图辅助项目及进度可视化显示.同时它又支持多项目管理.Redmine是一个自由开放 源码软件解决方案,它提供集成的项目 ...
- WebService wsdl没有types的问题
刚学WebService就遇到一个问题,按视屏敲的代码,它生成的wsdl页面与自己的不一样,我的没有types,结构完全不同了 This XML file does not appear to hav ...
- [Android Studio] Android studio 多渠道打包(超简洁版)
[Android Studio] Android studio 多渠道打包(超简洁版) 转载:http://xuyazhou.com/archives/461 http://relex.me/usin ...