配置Tomcat根目錄下/conf/server.xml文件:javascript
<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" 壓縮類型css
一旦啓用了這個壓縮功能後,怎麼來測試壓縮是否有效呢?首先Tomcat是根據瀏覽器請求頭中的accept-encoding來判斷瀏覽器是否支持壓縮功能,若是這個值包含有gzip,就代表瀏覽器支持gzip壓縮內容的瀏覽,下面分別有二段代碼進行tomcat內容是否壓縮過的測試程序。html
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(); } } }
執行這個測試程序,看看它所輸出的是什麼內容,若是輸出的是一些亂碼,以及打印內容的長度遠小於實際的長度,那麼恭喜你,你的配置生效了,你會發現你網站的瀏覽速度比之前快多了。 java
注:實際上是 tomcat 6 把註釋整個拿掉,讓你們覺得Tomcat對Gzip再也不支持,其實否則,你們能夠看一下http://tomcat.apache.org/tomcat-6.0-doc/config/http.html就知道,Tomcat依然支持這個功能。apache
原文地址:http://blog.csdn.net/xzknet/article/details/2800625json