httpClient4发送gzip的post数据,servlet接收并解压

1、gzipUtils工具类:

package nc.edu.nuc.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GzipUtils {
	public static String compress(String str) throws IOException {
		if (str == null || str.length() == 0) {
			return str;
		}
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		GZIPOutputStream gzip = new GZIPOutputStream(out);
		gzip.write(str.getBytes());
		gzip.close();
		return out.toString("ISO-8859-1"); // UTF-8 ISO-8859-1
	}
	// 解压缩
	public static String uncompress(String str) throws IOException {
		if (str == null || str.length() == 0) {
			return str;
		}
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ByteArrayInputStream in = new ByteArrayInputStream(
				str.getBytes("ISO-8859-1"));
		GZIPInputStream gunzip = new GZIPInputStream(in);
		byte[] buffer = new byte[256];
		int n;
		while ((n = gunzip.read(buffer)) >= 0) {
			out.write(buffer, 0, n);
		}
		// toString()使用平台默认编码,也可以显式的指定如toString("GBK")
		return out.toString();
	}
}

2、客户端:

pom.xml文件:

<dependency>
				<groupId>org.apache.httpcomponents</groupId>
				<artifactId>httpcore</artifactId>
				<version>4.4</version>
			</dependency>
			<dependency>
				<groupId>org.apache.httpcomponents</groupId>
				<artifactId>httpclient</artifactId>
				<version>4.5.1</version>
			</dependency>
			<dependency>
				  <groupId>org.apache.httpcomponents</groupId>
				  <artifactId>httpasyncclient</artifactId>
				  <version>4.1.1</version>
			</dependency>

代码:

public static void main(String[] args) throws IOException {

		String str = "msg=[{p1:\"12_22_235\",pu:\"1\",v:\"1.0.1\",net_work:\" \",ua_model:\" \",os_v:\"\",t:\"1\",rpage:\"1\",tm_1:2,tm_2:2,tm_3:2,tm_4:2,tm_5:2,rload:2},"
				+ "{p1:\"2_22_235\",pu:\"1\",v:\"1.0.1\",net_work:\" \",ua_model:\" \",os_v:\"\",t:\"1\",rpage:\"1\",tm_1:2,tm_2:2,tm_3:2,tm_4:2,tm_5:2,rload:2},"
				+ "{p1:\"2_22_235\",pu:\"1\",v:\"1.0.1\",net_work:\" \",ua_model:\" \",os_v:\"\",t:\"1\",rpage:\"1\",tm_1:2,tm_2:2,tm_3:2,tm_4:2,tm_5:2,rload:2}]";
		
		HttpClient httpClient = new DefaultHttpClient();
		HttpPost method = new HttpPost("http://localhost:8080/WebTest/ReciveAndUnzipServlet?a=test1");
		method.addHeader("Content-type","application/json; charset=ISO-8859-1");
		method.setHeader("Accept", "application/json");
		method.setHeader("Content-Encoding", "gzip");
		method.setEntity(new StringEntity(GzipUtils.compress(str), Charset.forName("ISO-8859-1")));

		HttpResponse response = httpClient.execute(method);
		
		int statusCode = response.getStatusLine().getStatusCode();
		
		if (statusCode != HttpStatus.SC_OK) {
			System.out.println("error...");
		}

		// Read the response body
		String body = EntityUtils.toString(response.getEntity());
		
		
		System.out.println(body);
	}

对key=value使用gzip进行压缩,同时设置header的Content-Encoding,发送post请求。

3、服务端:

/**
	 * 接受httpClient发过来的gzip压缩数据,解压,然后使用。
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String parameter = request.getParameter("a");
		System.out.println(parameter);
		
		String acceptjson = "";
		String contentEncoding = request.getHeader("Content-Encoding");
		if (null != contentEncoding && contentEncoding.indexOf("gzip") != -1) {
			ServletInputStream inputStream = request.getInputStream();
			if (inputStream !=null) {
				GZIPInputStream gzis = new GZIPInputStream(inputStream);
				InputStreamReader reader = new InputStreamReader(gzis);
				BufferedReader br = new BufferedReader(reader);

				StringBuffer sb = new StringBuffer("");
				String temp;
				while ((temp = br.readLine()) != null) {
					sb.append(temp);
				}
				
				br.close();
				acceptjson = sb.toString();
			} 
		} else {
			acceptjson = "no compress...";
		}
		
		PrintWriter out = response.getWriter();
		out.println(acceptjson);
		out.close();
	}

1)打印url传来的参数;

2)通过http的header中Content-Encoding判断是否压缩数据;

3)把压缩的数据解压,然后原封不动key=value的格式返回给客户端。

运行服务端,然后运行客户端,最后客户端返回:

msg=[{p1:"12_22_235",pu:"1",v:"1.0.1",net_work:" ",ua_model:" ",os_v:"",t:"1",rpage:"1",tm_1:2,tm_2:2,tm_3:2,tm_4:2,tm_5:2,rload:2},{p1:"2_22_235",pu:"1",v:"1.0.1",net_work:" ",ua_model:" ",os_v:"",t:"1",rpage:"1",tm_1:2,tm_2:2,tm_3:2,tm_4:2,tm_5:2,rload:2},{p1:"2_22_235",pu:"1",v:"1.0.1",net_work:" ",ua_model:" ",os_v:"",t:"1",rpage:"1",tm_1:2,tm_2:2,tm_3:2,tm_4:2,tm_5:2,rload:2}]

4)直接通过浏览器请求:http://localhost:8080/WebTest/ReciveAndUnzipServlet?a=test

在浏览器上显示:

no compress...

补充:

1)nginx中配置的gzip on,是指户请求的内容再发送到用户客户端之前,Nginx服务器会根据一些具体的策略实施压缩。

2)通常在server端需要解压gzip数据的时候,一般都是通过过滤器来完成,可以参考下面的链接

参考:http://blog.csdn.net/lcx46/article/details/29393307




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赶路人儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值