扎职粤语扎职被删床戏:关于Tomcat GZip的疑问 - JavaEye问答

来源:百度文库 编辑:中财网 时间:2024/05/01 15:34:46
为了减少网络流量
采用Tomcat Gzip压缩格式
Java代码
  1. compression="on"    
  2. compressionMinSize="2048"    
  3. noCompressionUserAgents="gozilla, traviata"    
  4. compressableMimeType="text/html,application/xhtml+xml,application/xml,text/xml,text/javascript,text/css,text/plain,application/x-javascript,application/javascript,text/xhtml,text/json,application/json,application/x-www-form-urlencoded,text/javaScript"  
 

其他都可以了,可是唯独 通过action 返回 json对象 不支持此压缩.
返回json 是采用
this.getResponse().setContentType("text/javascript;charset=utf-8");
方式传回的.

还试过web.xml 加

         action
         text/html

都不行(用的struts2,因此扩展名为action)

测试gzip 效果代码



Java代码
  1. import org.apache.commons.httpclient.HttpClient;   
  2. import org.apache.commons.httpclient.methods.GetMethod;   
  3.   
  4.   
  5. /**  
  6. * HTTP客户端测试类  
  7. * @author liudong  
  8. */  
  9. public class HttpTester {   
  10.   
  11. /**  
  12. * @param args  
  13. */  
  14. public static void main(String[] args) throws Exception{   
  15.     HttpClient http = new HttpClient   
  16.    GetMethod get = new GetMethod(url);   
  17.   
  18. try{   
  19.     get.addRequestHeader("accept-encoding""gzip,deflate");   
  20.     get.addRequestHeader("user-agent""Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Alexa Toolbar; Maxthon 2.0)");   
  21.     int er = http.executeMethod(get);   
  22.     if(er==200){   
  23.      //System.out.println(get.get);   
  24.      String html = get.getResponseBodyAsString();   
  25.      System.out.println(html.getBytes().length);   
  26.      System.out.println(html);   
  27.     }   
  28.    }finally{   
  29.     get.releaseConnection();   
  30.    }   
  31. }   
  32.   
  33. }  




              
问题补充
sdh5724 写道compressionMinSize="2048"  

大哥, 你json输出超过2K了么!!!!!!!!!!!!!!!!!
不要搞那么多!
何止2k 有时>1M
局域网,但有时网络也慢

问题补充:
谢谢回复
从firefox拿到的header 返回数据大概0.5M

响应头信息
Server Apache-Coyote/1.1

Transfer-Encoding chunked

Date Sun, 08 Mar 2009 11:58:34 GMT

请求头信息
Host 192.168.0.244:8088

User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1 zh-CN; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6

Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language zh-cn,zh;q=0.5

Accept-Encoding gzip,deflate

Accept-Charset gb2312,utf-8;q=0.7,*;q=0.7

Keep-Alive 300

Connection keep-alive

X-Requested-With XMLHttpRequest

Powered-By Ext

Content-Type application/x-www-form-urlencoded; charset=UTF-8

Referer
http://localhost/test/testGzip.action
Content-Length 121

Cookie JSESSIONID=6CB19BC17C83D1DF221258523C35C360

Pragma no-cache

Cache-Control no-cach
问题补充:
多谢 sdh5724
问题已解决
this.getResponse().setCharacterEncoding("utf-8");
this.getResponse().setContentType("text/javascript;charset=utf-8");
this.getResponse().getWriter().write(o.toString());

顺序应该在 this.getResponse().getWriter().write(o.toString());
之前

采纳的答案

2009-03-08 sdh5724 (资深程序员)

Server Apache-Coyote/1.1

Transfer-Encoding chunked

Date Sun, 08 Mar 2009 11:58:34 GMT

你这个返回头根本没有说是压缩,  你根本没有正确的输出文档类型。 所以, 你还是看看你的配置是否正确。 另外你需要在程序里指定, 你的输出是text/javascript 这样输出的时候tomcat 发现要压缩的类型,才能为你压缩。



提问者对于答案的评价:
多谢,这么晚还一直帮忙
额外加分:10