項目環境: 對接的服務放在微服務中 提供接口給應用層調用 ,微服務放需要 接受參數 並且轉換成壓縮格式給 第三方服務
本來以為需要自己壓縮,httpclint 中已經封裝好了GzipCompressingEntity 對象
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()));
}
