http 中指定head中Content-Encoding屬性為gzip 轉換過程中的一些問題


項目環境: 對接的服務放在微服務中 提供接口給應用層調用 ,微服務放需要 接受參數 並且轉換成壓縮格式給 第三方服務

本來以為需要自己壓縮,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()));
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM