博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Tomcat6启用Gzip压缩功能
阅读量:4689 次
发布时间:2019-06-09

本文共 3440 字,大约阅读时间需要 11 分钟。

  配置Tomcat根目录下/conf/server.xml文件:

1) compression="on" 打开压缩功能

2) compressionMinSize="2048" 启用压缩的输出内容大小,这里面默认为2KB
3) noCompressionUserAgents="gozilla, traviata" 
对于以下的浏览器,不启用压缩
 
4) compressableMimeType="text/html,text/xml"
 压缩类型

 

  一旦启用了这个压缩功能后,怎么来测试压缩是否有效呢?首先Tomcat是根据浏览器请求头中的accept-encoding来判断浏览器是否支持压缩功能,如果这个值包含有gzip,就表明浏览器支持gzip压缩内容的浏览,下面分别有二段代码进行tomcat内容是否压缩过的测试程序。

 

package com.triman.base.util;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import java.util.zip.GZIPInputStream;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpException;import org.apache.commons.httpclient.methods.GetMethod;public class TestTomcat {    public static void main(String [] args) throws HttpException, IOException{        HttpClient http = new HttpClient();        GetMethod get = new GetMethod("http://localhost:8080/sh_jasr/login.jsp");           try{                get.addRequestHeader("accept-encoding", "gzip,deflate");                get.addRequestHeader("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Alexa Toolbar; Maxthon 2.0)");               int er = http.executeMethod(get);               if(er==200){                   //第一种方案,采用读String的方式进行读取;//                    System.out.println(get.getResponseContentLength());//                    String html = get.getResponseBodyAsString();//                    System.out.println(html);//                    System.out.println(html.getBytes().length);                    //第二种方案,采用stream进行读,主要针对gzip解压;                    System.out.println("采用stream进行读,主要针对gzip解压;");                    InputStream in=get.getResponseBodyAsStream();                    // 对返回的stream流数据进行gzip解密;                    GZIPInputStream gzin = new GZIPInputStream(in);                    BufferedReader bin = new BufferedReader(new InputStreamReader(gzin,"UTF-8"));                    String s = null;                    while ((s = bin.readLine()) != null) {                        System.out.println(s);                    }                    bin.close();                    System.out.println("采用stream进行读,主要针对gzip解压;");               }          }finally{                  get.releaseConnection();          }    }    public static void main1(String[] args) {        try {            URL url = new URL("http://localhost:8080/sh_jasr/login.jsp");            HttpURLConnection conn = (HttpURLConnection) url.openConnection();            conn.setRequestProperty("Accept-Encoding", "gzip,deflate");// 如果这里不设置,返回的就不是gzip的数据了,也就不用解压缩了            conn.connect();            InputStream in = conn.getInputStream();            // BufferedReader bin = new BufferedReader(new InputStreamReader(in,"UTF-8"));            // 对返回的stream流数据进行gzip解压;            GZIPInputStream gzin = new GZIPInputStream(in);            BufferedReader bin = new BufferedReader(new InputStreamReader(gzin,"UTF-8"));            String s = null;            while ((s = bin.readLine()) != null) {                System.out.println(s);            }            bin.close();        } catch (Exception e) {            e.printStackTrace();        }    }}

 

   执行这个测试程序,看看它所输出的是什么内容,如果输出的是一些乱码,以及打印内容的长度远小于实际的长度,那么恭喜你,你的配置生效了,你会发现你网站的浏览速度比以前快多了。  

  注:其实是 tomcat 6 把注释整個拿掉,让大家以为TomcatGzip不再支持,其实不然,大家可以看一下就知道,Tomcat依然支持这个功能。

原文地址:

转载于:https://www.cnblogs.com/bingya/p/3503197.html

你可能感兴趣的文章
在线客服代码,可以用
查看>>
iPhone为何优越过 Android呢
查看>>
LeetCode Shortest Word Distance II
查看>>
XMLConfigBuilder文件
查看>>
外键的增删改查练习
查看>>
python 逻辑运算的短路问题
查看>>
专线维护 07/11
查看>>
ajax返回
查看>>
IE9兼容性视图与IE9标准视图
查看>>
sprint3个人总结
查看>>
七周七语言——Prolog(二)
查看>>
MLN Alchemy
查看>>
maven 的 oracle的Missing artifact com.oracle:******:jar:11.2.0.2.0
查看>>
vue服务端渲染添加缓存
查看>>
20165320 第七周学习总结
查看>>
安装及创建python虚拟环境
查看>>
数据库、C#、Java生成唯一GUID 方法
查看>>
gtest 安装
查看>>
sql中根据逗号分隔,查出多行数据
查看>>
js 回到頂部
查看>>