http 中指定head中Content-Encoding属性为gzip 转换过程中的一些问题

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

本来以为需要自己压缩,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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值