项目环境: 对接的服务放在微服务中 提供接口给应用层调用 ,微服务放需要 接受参数 并且转换成压缩格式给 第三方服务

本来以为需要自己压缩,httpclint 中已经封装好了GzipCompressingEntity 对象
```java
StringEntity entity = new StringEntity(json, "UTF-8");
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
httpPost.setHeader("ICK-Content-Encoding", "gzip");
httpPost.setEntity(new GzipCompressingEntity(entity));

```java
接收应用层参数还是需要压缩:这边需要注意的是 压缩完成之后得到的byte[] 需要Base64.encodeBase64() 解压后需要Base64.decodeBase64()才能顺利还原
public static String compress(String str, String encoding) {
if (str == null || str.length() == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip;
try {
gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes(encoding));
gzip.close();
} catch (Exception e) {
e.printStackTrace();
}
return new String(Base64.encodeBase64(out.toByteArray()));
}

转载于:https://www.cnblogs.com/jinjian91/p/10855640.html

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐