服务器使用Gzip压缩数据,加快网络传输(Java 例子)
2012-04-17 10:44
791 查看
在我们的项目中,添加对gzip的支持,是为了加快数据在网络中的传输速度。
使用gzip,首先要设置请求消息头Accept-Encoding为gzip。这样,你将会得到一个响应,根据消息头Content-Encoding为gzip你可以知道,传输过来的数据是经过gzip压缩的。另外,消息头Content-Length会告诉你压缩后的数据长度。
用Java实现的gzip
GetMethod method = new GetMethod(url);//生成一个get方法实例
method.setQueryString(queryString);//设置查询字符串
method.addRequestHeader("Accept-Encoding", "gzip");//设置接受响应消息为gzip
HttpClient client = new HttpClient();//生成执行get方法的客户端实例
client.executeMethod(method);//执行get方法
InputStream in = method.getResponseBodyAsStream();//获取响应消息体
Header contentEncoding = method.getResponseHeader("Content-Encoding");//获取消息头Content-Encoding判断数据流是否gzip压缩过
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
GZIPInputStream gzipIn = new GZIPInputStream(in);
int len = Integer.parseInt(method.getResponseHeader("Content-Length").getValue());
byte[] b = new byte[len];
gzipIn.read(b);
String json = new String(b);
System.out.println(json);
}
使用gzip在服务器端压缩数据的例子。
byte[] result = data.getBytes("UTF-8");
if(response.getHeader("Accept-Encoding").equalsIgnoreCase("gzip"))
{
// System.out.println("Before compression, the data size is :"+ result.length);
// Using gzip compress the data
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gout = new GZIPOutputStream(out);
gout.write(json.getBytes("UTF-8"));
gout.close();
result = out.toByteArray();
// System.out.println("After compression, the data size is "+gzipResult.length);
this.getResp().setHeader("Content-Encoding","gzip");
this.getResp().setHeader("Content-Length", result.length+"");
}
response.getOutputStream().write(result);
使用gzip,首先要设置请求消息头Accept-Encoding为gzip。这样,你将会得到一个响应,根据消息头Content-Encoding为gzip你可以知道,传输过来的数据是经过gzip压缩的。另外,消息头Content-Length会告诉你压缩后的数据长度。
用Java实现的gzip
GetMethod method = new GetMethod(url);//生成一个get方法实例
method.setQueryString(queryString);//设置查询字符串
method.addRequestHeader("Accept-Encoding", "gzip");//设置接受响应消息为gzip
HttpClient client = new HttpClient();//生成执行get方法的客户端实例
client.executeMethod(method);//执行get方法
InputStream in = method.getResponseBodyAsStream();//获取响应消息体
Header contentEncoding = method.getResponseHeader("Content-Encoding");//获取消息头Content-Encoding判断数据流是否gzip压缩过
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
GZIPInputStream gzipIn = new GZIPInputStream(in);
int len = Integer.parseInt(method.getResponseHeader("Content-Length").getValue());
byte[] b = new byte[len];
gzipIn.read(b);
String json = new String(b);
System.out.println(json);
}
使用gzip在服务器端压缩数据的例子。
byte[] result = data.getBytes("UTF-8");
if(response.getHeader("Accept-Encoding").equalsIgnoreCase("gzip"))
{
// System.out.println("Before compression, the data size is :"+ result.length);
// Using gzip compress the data
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gout = new GZIPOutputStream(out);
gout.write(json.getBytes("UTF-8"));
gout.close();
result = out.toByteArray();
// System.out.println("After compression, the data size is "+gzipResult.length);
this.getResp().setHeader("Content-Encoding","gzip");
this.getResp().setHeader("Content-Length", result.length+"");
}
response.getOutputStream().write(result);
相关文章推荐
- 服务器使用Gzip压缩数据,加快网络传输(Java 例子)
- 服务器使用Gzip压缩数据,加快网络传输(Java 例子)
- 服务器使用Gzip压缩数据,加快网络传输(Java 例子)
- 服务器使用Gzip压缩数据,加快网络传输(Java 例子)
- 使用Java实现网络传输数据的压缩
- 使用Java实现网络传输数据的压缩.
- 在Unity3d中使用GZip来压缩网络传输数据
- android接口中json数据的传输中使用gzip压缩
- Android与Java 服务器使用Socket协议实现Json数据传输
- Java GZip数据压缩传输到前台
- 在Unity3d中使用GZip来压缩传输数据
- 使用Gzip压缩数据,加快页面访问速度
- 减小网络传输图片大小,使用gzip进行压缩
- 使用GZIP压缩的方式向客户端传输响应数据
- java服务器对json数据进行gzip压缩
- 在Unity3d中使用GZip来压缩传输数据
- Java使用GZIP进行压缩和解压
- Android开发中网络请求的压缩 ── GZip的使用
- 是否需要对网络传输数据进行压缩?如何选择压缩算法?
- php curl采集,服务器gzip压缩返回数据怎么办