服务器使用Gzip压缩数据,加快网络传输(Java 例子)

摘要:
在我们的项目中,添加对gzip的支持,是为了加快数据在网络中的传输速度。Ifyouneedtotransferdatausinggzip,youmustbesettingrequestheader"Accept-Encoding"="gzip".Thus,youwillgetaresponse,whichincludetheresponseheadername"Content-Encoding"andvalue"gzip",andneedtoungziptheresponsedata.Besides,theresponseheadername"Content-Length"alsowillbereturned.使用gzip,首先要设置请求消息头Accept-Encoding为gzip。这样,你将会得到一个响应,根据消息头Content-Encoding为gzip你可以知道,传输过来的数据是经过gzip压缩的。另外,消息头Content-Length会告诉你压缩后的数据长度。使用gzip在服务器端压缩数据的例子。
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
  1. GetMethodmethod=newGetMethod(url);//生成一个get方法实例
  2. method.setQueryString(queryString);//设置查询字符串
  3. method.addRequestHeader("Accept-Encoding","gzip");//设置接受响应消息为gzip
  4. HttpClientclient=newHttpClient();//生成执行get方法的客户端实例
  5. client.executeMethod(method);//执行get方法
  6. InputStreamin=method.getResponseBodyAsStream();//获取响应消息体
  7. HeadercontentEncoding=method.getResponseHeader("Content-Encoding");//获取消息头Content-Encoding判断数据流是否gzip压缩过
  8. if(contentEncoding!=null&&contentEncoding.getValue().equalsIgnoreCase("gzip")){
  9. GZIPInputStreamgzipIn=newGZIPInputStream(in);
  10. intlen=Integer.parseInt(method.getResponseHeader("Content-Length").getValue());
  11. byte[]b=newbyte[len];
  12. gzipIn.read(b);
  13. Stringjson=newString(b);
  14. System.out.println(json);
  15. }

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

  1. byte[]result=data.getBytes("UTF-8");
  2. if(response.getHeader("Accept-Encoding").equalsIgnoreCase("gzip"))
  3. {
  4. //System.out.println("Beforecompression,thedatasizeis:"+result.length);
  5. //Usinggzipcompressthedata
  6. ByteArrayOutputStreamout=newByteArrayOutputStream();
  7. GZIPOutputStreamgout=newGZIPOutputStream(out);
  8. gout.write(json.getBytes("UTF-8"));
  9. gout.close();
  10. result=out.toByteArray();
  11. //System.out.println("Aftercompression,thedatasizeis"+gzipResult.length);
  12. this.getResp().setHeader("Content-Encoding","gzip");
  13. this.getResp().setHeader("Content-Length",result.length+"");
  14. }
  15. response.getOutputStream().write(result);

免责声明:文章转载自《服务器使用Gzip压缩数据,加快网络传输(Java 例子)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇比特币的锁定脚本与解锁脚本算符优先分析下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

CentOS7安装zabbix

二进制包安装    centos 7 添加阿里云镜像 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo yum install epel-release 安装常用的开发组件 yum groups install "Develo...

Postgresql在线备份和恢复

1.实验环境 OS: RedHat Linux Enterprisedb 6.3 DB: postgresql 9.3 PGHOME: /opt/PostgreSQL/9.3 PGDATA: /opt/PostgreSQL/9.3/data 归档目录:/opt/pg_archive 基础备份目录:/opt/base_archive --生产环境中归档和数...

ipfs存储和获取数据

安装ipfs-api 切换到项目根目录,安装ipfs-api。 npm install --save ipfs-api   导入IPFS const ipfsAPI = require('ipfs-api'); const ipfs = ipfsAPI({host: 'localhost', port: '5001', protocol: 'http'})...

关于jQuery中的attr和data问题

今天在使用data获取属性并且赋值时遇到一个小问题,写下来防止以后再跳坑。 在使用jQuery获取自定义属性值时,我们习惯用 $(selector).attr('data-value'); jQuery赋值: $(selector).attr('data-value','123456'); 而data的取值: $(selector).data('value...

JAVA 调用HTTP接口POST或GET实现方式(转)

HTTP是一个客户端和服务器端请求和应答的标准(TCP),客户端是终端用户,服务器端是网站。通过使用Web浏览器、网络爬虫或者其它的工具,客户端发起一个到服务器上指定端口(默认端口为80)的HTTP请求。 具体POST或GET实现代码如下: packagecom.yoodb.util; importjava.io.ByteArrayOutputStre...

你不知道的 Blob

来自公众号:全栈修仙之路 如果你允许用户从你的网站上下载某些文件,那你可能会遇到 Blob 类型。为了实现上述的功能,你可以很容易从网上找到相关的示例,并根据实际需求进行适当的调整。对于部分开发者来说,在完成上述功能之后,他们并不会继续思考 Blob 是什么? 这就导致了一些开发者,还是停留在熟练使用 API 的层面,当遇到比较棘手的问题时,就束手无策...