为了减少网络流量
采用Tomcat Gzip压缩格式
compression="on"
compressionMinSize="2048"
noCompressionUserAgents="gozilla, traviata"
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 加
<mime-mapping>
<extension>action</extension>
<mime-type>text/html</mime-type>
</mime-mapping>
都不行(用的struts2,因此扩展名为action)
测试gzip 效果代码
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;/**
- HTTP客户端测试类
- @author liudong */ public class HttpTester {
/**
- @param args */ public static void main(String[] args) throws Exception{ HttpClient http = new HttpClient GetMethod get = new GetMethod(url);
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){
//System.out.println(get.get);
String html = get.getResponseBodyAsString();
System.out.println(html.getBytes().length);
System.out.println(html);
}
}finally{
get.releaseConnection();
}
}
}
问题补充
大哥, 你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());
之前