过滤器-->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压缩的.如果没有,则可以再功能视图下找到“压缩”项,进入之后就会看到“静态内 ...
随机推荐
- [译]OpenStack Object Storage Monitoring
注:翻译的不完整,主要是有些地方翻译后反而妨碍理解,有些不知道怎么翻,anyway,需要时拿来用用也是可行的,顺便共享啦.欢迎提意见. 一个OpenStack Object Storage(OSOS) ...
- selenium遇到异常自动截图
最近要在框架中添加case失败时,要自动截图,主要又两种方式,思想都是在抛异常的时候,捕获到异常,并作页面截图处理.今天坐下总结. 一.第一种方式,重写onException方法 只针对webdriv ...
- Laxcus大数据管理系统2.0(5)- 第三章 数据存取
第三章 数据存取 当前的很多大数据处理工作,一次计算产生几十个GB.或者几十个TB的数据已是正常现象,驱动数百.数千.甚至上万个计算机节点并行运行也已经不足为奇.但是在数据处理的后面,对于这种在网络间 ...
- 边工作边刷题:70天一遍leetcode: day 88
Unique Word Abbreviation 要点: 简单题,主要是理解题意.no other words have the same abbreviation as the given word ...
- python如何控制数据库?
http://www.w3cschool.cc/python/python-mysql.html 通过利用MySQLdb可以操作数据库 实例: 以下实例链接Mysql的TESTDB数据库: # enc ...
- Java语法基础(一)----关键字、标识符、常量、变量
一.关键字: 关键字:被Java语言赋予特定含义的单词.组成关键字的字母全部小写.注:goto和const作为保留字存在,目前并不使用.main并不是关键字. 二.标识符: 标识符:就是给类,接口,方 ...
- java8-2 多态的概述
1.多态:同一个对象(事物),在不同时刻体现出来的不同状态. 举例: 猫是猫,猫是动物. 水(液体,固体,气态). 多态的前提: A:要有继承关系. B:要有方法重写. 其实没有也是可以的,但是如果没 ...
- HTML5和css3的总结三
继续总结H5的新东西 1>序列化与反序列化 序列化:其实就是一个json->string的过程 JSON.stringify(json); 反序列化:string->json的过程( ...
- ABP入门系列(5)——创建应用服务
一.解释下应用服务层 应用服务用于将领域(业务)逻辑暴露给展现层.展现层通过传入DTO(数据传输对象)参数来调用应用服务,而应用服务通过领域对象来执行相应的业务逻辑并且将DTO返回给展现层.因此,展现 ...
- DevExpress主从表 按组分页一组不足一页为一页--以此记录
本文的主要是说明Dev的报表的主从表,主从表的每一组显示在一页,当一组超出一页,第二页只显示第一组的. 一.每上报表设置图 简单设计图如上 二.后台代码 报表页代码 public partial cl ...