|
为了减少网络流量
采用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();
}
}
} |
|
|