½ÓÏÂÀ´£¬ÈÃÎÒ½â˵һÏÂÈçºÎÔÚAndroidϵͳÖÐʹÓÃgzip½øÐÐÊý¾Ý´«µÝ
HTTPÐÒéÉϵÄGZIP±àÂëÊÇÒ»ÖÖÓÃÀ´¸Ä½øWEBÓ¦ÓóÌÐòÐÔÄܵļ¼Êõ¡£´óÁ÷Á¿µÄWEBÕ¾µã³£³£Ê¹ÓÃGZIPѹËõ¼¼ÊõÀ´¼õÉÙÎļþ´óС£¬¼õÉÙÎļþ´óСÓÐÁ½¸öÃ÷ÏԵĺô¦£¬Ò»ÊÇ¿ÉÒÔ¼õÉÙ´æ´¢¿Õ¼ä£¬¶þÊÇͨ¹ýÍøÂç´«ÊäÎļþʱ£¬¿ÉÒÔ¼õÉÙ´«ÊäµÄʱ¼ä¡£×÷ÕßÔÚдÕâƪ²©¿Íʱ¾¹ý²âÊÔ£¬4.4MBµÄÎı¾Êý¾Ý¾¹ýGzip´«Êäµ½¿Í»§¶ËÖ®ºó±äΪ392KB£¬Ñ¹ËõЧÂʼ«¸ß¡£
Ò».·þÎñ¶Ë
·þÎñ¶ËÓÐ2ÖÖ·½Ê½È¥Ñ¹Ëõ£¬Ò»ÖÖ¿ÉÒÔ×Ô¼ºÑ¹Ëõ£¬µ«ÊǸüÍƼöµÚ¶þÖÖ·½Ê½£¬ÓÃPrintWriter×÷ΪÊä³öÁ÷£¬¹¤¾ßÀà´úÂëÈçÏÂ
¸´ÖÆ´úÂë ´úÂëÈçÏÂ:
/**
* ÅжÏä¯ÀÀÆ÷ÊÇ·ñÖ§³Ö gzip ѹËõ
* @param req
* @return boolean Öµ
*/
public static boolean isGzipSupport(HttpServletRequest req) {
String headEncoding = req.getHeader("accept-encoding");
if (headEncoding == null || (headEncoding.indexOf("gzip") == -1)) { // ¿Í»§¶Ë ²»Ö§³Ö gzip
return false;
} else { // Ö§³Ö gzip ѹËõ
return true;
}
}
/**
* ´´½¨ ÒÔ gzip ¸ñʽ Êä³öµÄ PrintWriter ¶ÔÏó£¬Èç¹ûä¯ÀÀÆ÷²»Ö§³Ö gzip ¸ñʽ£¬Ôò´´½¨ÆÕͨµÄ PrintWriter ¶ÔÏó£¬
* @param req
* @param resp
* @return
* @throws IOException
*/
public static PrintWriter createGzipPw(HttpServletRequest req, HttpServletResponse resp) throws IOException {
PrintWriter pw = null;
if (isGzipSupport(req)) { // Ö§³Ö gzip ѹËõ
pw = new PrintWriter(new GZIPOutputStream(resp.getOutputStream()));
// ÔÚ header ÖÐÉèÖ÷µ»ØÀàÐÍΪ gzip
resp.setHeader("content-encoding", "gzip");
} else { // // ¿Í»§¶Ë ²»Ö§³Ö gzip
pw = resp.getWriter();
}
return pw;
}
servlet´úÂëÈçÏ£º
¸´ÖÆ´úÂë ´úÂëÈçÏÂ:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Encoding", "gzip");
String ret = "{"ContentLayer":{"title":"ÄÚÈݲã"},"PageLink":{"title":"Ò³ÃæÌøת"},"WebBrowser":{"title":"ä¯ÀÀÆ÷"},"
+ ""InlinePage":{"title":"ÄÚǶҳÃæ"},"VideoComp":{"title":"ÊÓƵ"},"
+ ""PopButton":{"title":"ÄÚÈÝ¿ª¹Ø"},"ZoomingPic":{"title":"Ëõ·Å´óͼ"},"
+ ""Rotate360":{"title":"360¶ÈÐýת"}}";
PrintWriter pw = new PrintWriter(new GZIPOutputStream(response.getOutputStream()));
pw.write(ret);
pw.close();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
ÔÚ´úÀíÈí¼þÖиú×Ùµ½µÄÊý¾ÝÈçÏ£º
¸´ÖÆ´úÂë ´úÂëÈçÏÂ:
‹«VrÎÏ+IÍ+ñI¬L-R²ªV*É,ÉIU²R:rëÄÝM•ju¡±ÓS}2¨®²¡®e/m>¨¹¨¬Ìë«@¨°¨¢©INE¨´åÅ¡§¨²Ÿ¬?p¨¤Øw¼g^Nf^*ÈT¨®o™R¨C™'šïœŸ[¬¨¤Ôåc[ÁÖç8•¨C¡±äç¡»nÿª7@
¢¨°¨®s3¨®Ò2¡°¡®Uœþºý¨¨¨CÏg¡ÂT塪$¨C¡è› +r¡¤¸ðä‡Zh¡è†ˆ
ʵ¼ÊÊý¾ÝÈçÏ£º
¸´ÖÆ´úÂë ´úÂëÈçÏÂ:
{"ContentLayer":{"title":"ÄÚÈݲã"},"PageLink":{"title":"Ò³ÃæÌøת"},"WebBrowser":{"title":"ä¯ÀÀÆ÷"},"InlinePage":{"title":"ÄÚǶҳÃæ"},"VideoComp":{"title":"ÊÓƵ"},"PopButton":{"title":"ÄÚÈÝ¿ª¹Ø"},"ZoomingPic":{"title":"Ëõ·Å´óͼ"},"Rotate360":{"title":"360¶ÈÐýת"}}