如果希望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 閱讀(1024)
評論(0) 編輯 收藏 所屬分類:
JAVA