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()));
}
最新文章
- 【Android】开发中个人遇到和使用过的值得分享的资源合集
- 深入理解 JavaScript 变量的作用域和作用域链
- 自然语言9_NLTK计算中文高频词
- gitlab的使用方法
- Origin9.1如何使用原始数据(Raw Data)绘制风向玫瑰图
- SVN用法(merge,branch,switch)
- 5 HBase 常用Shell命令
- 桦仔 笔记7-徐 SQLSERVER日志记录机制
- Redis 中的数据类型及基本操作
- Xcode7.2中如何添加一个Empty Application模板
- FCC(ES6写法) Validate US Telephone Numbers
- 【Atcoder Grand Contest 011 F】Train Service Planning
- F - Count the Colors ZOJ - 1610 线段树染色(染区间映射)
- SQL LITE安装
- [SQL]查询整个数据库中某个特定值所在的表和字段的方法
- 【2017-02-28】C# 冒泡排序
- ACM-ICPC 2018 北京赛区网络预赛(9.22)
- 解决eclipse新建项目看不到src/main/java目录办法
- 后Hadoop时代的大数据技术思考:数据即服务
- 【Linux】进程优先级、进程nice值和%nice