zoukankan      html  css  js  c++  java
  • 服务器使用Gzip压缩数据,加快网络传输(Java 例子)

    The next version of the Project will provide support for gzip in order to faster speed of data transmission on the network。在我们的项目中,添加对gzip的支持,是为了加快数据在网络中的传输速度。


    If you need to transfer data using gzip, you must be setting request header "Accept-Encoding"="gzip". Thus,  you will get a response, which include the response header name "Content-Encoding" and value "gzip", and need to ungzip the response data. Besides, the response header name "Content-Length" also will be returned.使用gzip,首先要设置请求消息头Accept-Encoding为gzip。这样,你将会得到一个响应,根据消息头Content-Encoding为gzip你可以知道,传输过来的数据是经过gzip压缩的。另外,消息头Content-Length会告诉你压缩后的数据长度。
       
       A example using gzip implemented by Java。用Java实现的gzip
    [html] view plain copy
     print?
    1. GetMethod method = new GetMethod(url);//生成一个get方法实例  
    2.   
    3. method.setQueryString(queryString);//设置查询字符串  
    4. method.addRequestHeader("Accept-Encoding", "gzip");//设置接受响应消息为gzip  
    5.   
    6. HttpClient client = new HttpClient();//生成执行get方法的客户端实例  
    7. client.executeMethod(method);//执行get方法  
    8.   
    9. InputStream in = method.getResponseBodyAsStream();//获取响应消息体  
    10. Header contentEncoding = method.getResponseHeader("Content-Encoding");//获取消息头Content-Encoding判断数据流是否gzip压缩过  
    11.   
    12. if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {  
    13.     GZIPInputStream gzipIn = new GZIPInputStream(in);  
    14.     int len = Integer.parseInt(method.getResponseHeader("Content-Length").getValue());  
    15.     byte[] b = new byte[len];  
    16.     gzipIn.read(b);  
    17.       
    18.     String json = new String(b);  
    19.     System.out.println(json);  
    20. }  

        In addtional, give a example about Server compress the data using gzip。使用gzip在服务器端压缩数据的例子。

    [html] view plain copy
     print?
    1. byte[] result = data.getBytes("UTF-8");  
    2.   
    3. if(response.getHeader("Accept-Encoding").equalsIgnoreCase("gzip"))  
    4. {  
    5.     // System.out.println("Before compression, the data size is :"+ result.length);  
    6.     // Using gzip compress the data  
    7.     ByteArrayOutputStream out = new ByteArrayOutputStream();  
    8.     GZIPOutputStream gout = new GZIPOutputStream(out);  
    9.     gout.write(json.getBytes("UTF-8"));  
    10.     gout.close();  
    11.     result = out.toByteArray();  
    12.   
    13.     // System.out.println("After compression, the data size is "+gzipResult.length);  
    14.     this.getResp().setHeader("Content-Encoding","gzip");  
    15.     this.getResp().setHeader("Content-Length", result.length+"");  
    16. }  
    17.   
    18. response.getOutputStream().write(result);  
  • 相关阅读:
    element-ui 后台问题
    element-ui 使用:rules对表单字段进行验证
    扫码枪 移动端监听
    前端好用工具介绍——wulihub
    移动端 扫描枪输入不弹出键盘
    移动端 input输入实时监听查询数据渲染
    新老系统统一认证解决方案
    转 高性能IO模型浅析
    数据库备份方案
    软件开发阶段数据库升级维护策略
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13317731.html
Copyright © 2011-2022 走看看