欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在Android系統(tǒng)中使用gzip進(jìn)行數(shù)據(jù)傳遞實(shí)例代碼

 更新時(shí)間:2013年06月07日 16:35:53   作者:  
HTTP協(xié)議上的GZIP編碼是一種用來改進(jìn)WEB應(yīng)用程序性能的技術(shù),4.4MB的文本數(shù)據(jù)經(jīng)過Gzip傳輸?shù)娇蛻舳酥笞優(yōu)?92KB,壓縮效率極高,下面與大家分享下具體的實(shí)現(xiàn)
接下來,讓我解說一下如何在Android系統(tǒng)中使用gzip進(jìn)行數(shù)據(jù)傳遞
HTTP協(xié)議上的GZIP編碼是一種用來改進(jìn)WEB應(yīng)用程序性能的技術(shù)。大流量的WEB站點(diǎn)常常使用GZIP壓縮技術(shù)來減少文件大小,減少文件大小有兩個(gè)明顯的好處,一是可以減少存儲(chǔ)空間,二是通過網(wǎng)絡(luò)傳輸文件時(shí),可以減少傳輸?shù)臅r(shí)間。作者在寫這篇博客時(shí)經(jīng)過測(cè)試,4.4MB的文本數(shù)據(jù)經(jīng)過Gzip傳輸?shù)娇蛻舳酥笞優(yōu)?92KB,壓縮效率極高。

一.服務(wù)端
服務(wù)端有2種方式去壓縮,一種可以自己壓縮,但是更推薦第二種方式,用PrintWriter作為輸出流,工具類代碼如下
復(fù)制代碼 代碼如下:

/**
* 判斷瀏覽器是否支持 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;
}
}
/**
* 創(chuàng)建 以 gzip 格式 輸出的 PrintWriter 對(duì)象,如果瀏覽器不支持 gzip 格式,則創(chuàng)建普通的 PrintWriter 對(duì)象,
* @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 中設(shè)置返回類型為 gzip
resp.setHeader("content-encoding", "gzip");
} else { // // 客戶端 不支持 gzip
pw = resp.getWriter();
}
return pw;
}

servlet代碼如下:
復(fù)制代碼 代碼如下:

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Encoding", "gzip");
String ret = "{\"ContentLayer\":{\"title\":\"內(nèi)容層\"},\"PageLink\":{\"title\":\"頁面跳轉(zhuǎn)\"},\"WebBrowser\":{\"title\":\"瀏覽器\"},"
+ "\"InlinePage\":{\"title\":\"內(nèi)嵌頁面\"},\"VideoComp\":{\"title\":\"視頻\"},"
+ "\"PopButton\":{\"title\":\"內(nèi)容開關(guān)\"},\"ZoomingPic\":{\"title\":\"縮放大圖\"},"
+ "\"Rotate360\":{\"title\":\"360度旋轉(zhuǎn)\"}}";
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);
}

在代理軟件中跟蹤到的數(shù)據(jù)如下:
復(fù)制代碼 代碼如下:

‹«VrÎÏ+IÍ+ñI¬L-R²ªV*É,ÉIU²R:rëÄÝM•ju”ÓS}2ó²‘e/m>üì̏ë«@òá©INEùåŨúŸ¬?pàØw¼g^Nf^*ÈTóo™R–™'šïœŸ[€¬àÔåc[ÁÖç8•–”äç¡»nÿª7@
¢òós3óÒ2“‘Uœþºýè–Ïg÷€Tå—$–¤› +r·¸ðä‡Zh¤†ˆ

實(shí)際數(shù)據(jù)如下:
復(fù)制代碼 代碼如下:

{"ContentLayer":{"title":"內(nèi)容層"},"PageLink":{"title":"頁面跳轉(zhuǎn)"},"WebBrowser":{"title":"瀏覽器"},"InlinePage":{"title":"內(nèi)嵌頁面"},"VideoComp":{"title":"視頻"},"PopButton":{"title":"內(nèi)容開關(guān)"},"ZoomingPic":{"title":"縮放大圖"},"Rotate360":{"title":"360度旋轉(zhuǎn)"}}

相關(guān)文章

最新評(píng)論