如果希望http response 总是能压缩后传给客户端, 需要在服务端对客户的请求进行检测, 看是否有 accept-encoding = gzip 的头, 一般用浏览器做客户端,都回包含accept-encoding 的头, 是否压缩response 就由服务器自己决定, 但是如果是其他程序做客户端, 可能不包含accept-encoding 头, 所以在服务器端要进行检查
1
protected void doPost(HttpServletRequest req, HttpServletResponse res)
2
throws ServletException, IOException
{
3
4
if(!isAcceptCompression(req))
5
error handle
6
7
super.doPost(req, res);
8
}
9
10
private static String ACCEPT_COMPRESSION_HEADER = "accept-encoding";
11
private static String ACCEPT_COMPRESSION_GZIP = "gzip";
12
protected boolean isAcceptCompression(HttpServletRequest req)
13
{
14
java.util.Enumeration en = req.getHeaderNames();
15
while (en.hasMoreElements())
16
{
17
String headerName = (String)en.nextElement();
18
String headerValue = req.getHeader(headerName);
19
if(ACCEPT_COMPRESSION_HEADER.equalsIgnoreCase(headerName))
20
{
21
if((headerValue!= null) && ((headerValue.toLowerCase().indexOf(ACCEPT_COMPRESSION_GZIP) >=0)))
22
return true;
23
}
24
}
25
return false;
26
}
posted on 2008-11-14 15:05
happyy2k 阅读(1020)
评论(0) 编辑 收藏 所属分类:
JAVA