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

本来以为需要自己压缩,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()));
    }
posted on 2019-05-13 11:20  爱不死  阅读(10540)  评论(0编辑  收藏  举报