• Tomcat6启用Gzip压缩功能


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

    <Connector port="8080" protocol="HTTP/1.1" 
                   connectionTimeout="20000" 
                   redirectPort="8443" URIEncoding="UTF-8"
                   compression="on"
                   compressionMinSize="2048" 
                   noCompressionUserAgents="gozilla,traviata"
                   compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain,application/json"
                   />

    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不再支持,其实不然,大家可以看一下http://tomcat.apache.org/tomcat-6.0-doc/config/http.html就知道,Tomcat依然支持这个功能。

    原文地址:http://blog.csdn.net/xzknet/article/details/2800625

  • 相关阅读:
    Ajax World 名人堂
    Chrome Flash插件的手动安装方法
    2009年郭红俊的工作
    动画缓冲或叫缓动函数(Animation Easing)
    2008年郭红俊的工作
    Android下调整多媒体音量方法
    游戏中按概率播放某个音效简单c++实现
    string转int 等(转)
    .NET简谈面“.NET技术”向接口编程 狼人:
    改善代码设计 —— 处理概括关系(Dealing w“.NET技术”ith Generalization) 狼人:
  • 原文地址:https://www.cnblogs.com/bingya/p/3503197.html
Copyright © 2020-2023  润新知