tomcat下Gzip压缩
来源:互联网 发布:数据精灵9.8.2注册机 编辑:程序博客网 时间:2024/05/16 09:52
tomcat下Gzip压缩
HTTP 压缩可以大大提高浏览网站的速度,它的原理是,在客户端请求服务器对应资源后,从服务器端将资源文件压缩,再输出到客户端,由客户端的浏览器负责解压缩并浏览。相对于普通的浏览过程HTML ,CSS,Javascript , Text ,它可以节省40%左右的流量。更为重要的是,它可以对动态生成的,包括CGI、PHP , JSP , ASP , Servlet,SHTML等输出的网页也能进行压缩,压缩效率也很高
使用方法:在tomcat的server.xml文件中:
1
<Connector port=
"8080"
protocol=
"HTTP/1.1"
2
connectionTimeout=
"20000"
3
redirectPort=
"8443"
4
compression=
"on"
5
compressionMinSize=
"2048"
6
noCompressionUserAgents=
"gozilla, traviata"
7
compressableMimeType=
"text/html,text/xml,text/javascript,text/css,text/plain"
8
/>
compression="on"
compressionMinSize="2048"
noCompressionUserAgents="gozilla, traviata"
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"
为添加的内容:
compression="on" 为启用tomcat的gzip压缩
compressionMinSize="2048" 为当文件大小不小于2kb时候开始压缩
noCompressionUserAgents="gozilla, traviata" 为不支持压缩的浏览器
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain" 为支持压缩的文件格式
java测试类:
01
package
com.best.oa.workshop.gzip;
02
03
import
org.apache.commons.httpclient.HttpClient;
04
import
org.apache.commons.httpclient.methods.GetMethod;
05
/**
06
* Created with IntelliJ IDEA.
07
* User: test
08
* Date: 13-9-24
09
* Time: 下午4:14
10
* To change this template use File | Settings | File Templates.
11
*/
12
public
class
Gzip {
13
public
static
void
main(String[] args)
throws
Exception{
14
HttpClient http =
new
HttpClient();
15
GetMethod get =
new
GetMethod(
"http://localhost:8080/login.htm"
);
16
try
{
17
get.addRequestHeader(
"accept-encoding"
,
"gzip,deflate"
);
18
get.addRequestHeader(
"user-agent"
,
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Alexa Toolbar; Maxthon 2.0)"
);
19
int
er = http.executeMethod(get);
20
if
(er==
200
){
21
System.out.println(get.getResponseContentLength());
22
String html = get.getResponseBodyAsString();
23
System.out.println(html);
24
System.out.println(html.getBytes().length);
25
}
26
}
finally
{
27
get.releaseConnection();
28
}
29
}
30
}
检测自己的网站是否开启Gzip开启压缩功能呢?
http://seo.chinaz.com
网址中输入自己的网站域名:
我的网站测试结果:
转自:http://my.oschina.net/baishi/blog/163877
- tomcat下Gzip压缩
- Tomcat配置Gzip压缩
- tomcat gzip压缩
- tomcat GZIP 压缩参数设置
- tomcat的gzip压缩
- tomcat启用gzip压缩
- Tomcat 启动gzip压缩
- Tomcat配置gzip压缩
- tomcat启用gzip压缩
- Tomcat开启Gzip压缩
- Tomcat 开启Gzip压缩
- Tomcat启用GZip压缩
- tomcat 加 gzip 压缩
- Tomcat 启动GZIP 压缩
- tomcat启用GZIP压缩
- Tomcat gzip压缩
- tomcat启用gzip压缩
- Tomcat启用Gzip压缩
- 解决RedHat操作系统终端显示出现乱码的问题
- http协议学习系列
- codeforces 394A Counting Sticks(题目虽简单,但是考虑的情况多,需仔细)
- 虚拟机里的CentOS 6直接克隆或拷贝后无法识别eth0
- oracle11g rac 实例1不能启动 ORA-01078
- tomcat下Gzip压缩
- Stacked Autoencoders
- Unity3D-深入剖析NGUI的游戏UI架构
- SGLTE and SVLTE
- OpenCV鼠标画图例程,鼠标绘制矩形
- (转)getSharePreference和getDefaultSharePreference的区别
- C#入门讲解(二)
- 关于筛选输入设备的设备节点
- ffmpeg文档学习