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文件:


				org.apache.httpcomponents
				httpcore
				4.4
			
			
				org.apache.httpcomponents
				httpclient
				4.5.1
			
			
				  org.apache.httpcomponents
				  httpasyncclient
				  4.1.1
			

代码:

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




你可能感兴趣的:(java)