ÔÚAndroidϵͳÖÐʹÓÃgzip½øÐÐÊý¾Ý´«µÝʵÀý´úÂë
ÔÚAndroidϵͳÖÐʹÓÃgzip½øÐÐÊý¾Ý´«µÝʵÀý´úÂë
发布时间:2016-12-28 来源:查字典编辑
摘要:½ÓÏÂÀ´£¬ÈÃÎÒ½â˵һÏÂÈçºÎÔÚAndroidϵͳÖÐʹÓÃgzip½øÐÐÊý¾Ý´«µÝHTTPЭÒéÉϵ...

½ÓÏÂÀ´£¬ÈÃÎÒ½â˵һÏÂÈçºÎÔÚ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¶ÈÐýת"}}

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新安卓软件开发学习
热门安卓软件开发学习
编程开发子分类