码迷,mamicode.com
首页 > 其他好文 > 详细

接口开发遇到BASE64编码并且报文被压缩的情况怎么办

时间:2020-07-22 20:26:49      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:server   UNC   utf-8   zip   set   cep   array   cache   sed   

1:核心代码

JSONObject json=(JSONObject) JSON.parse(responseContent);
byte [] compress=Base64.decodeBase64((json.getString("result").getBytes("UTF-8")));    (org.apache.commons.net.util.Base64)

byte [] umcompress=GZipUtil.uncompress(compress);
String jsons=new String(umcompress,"UTF-8");

 

2:解压工具类代码如下:

package com.trafree.banking.server.hardcore.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;

public class GZipUtil {

/**
* 解压
* 使用gzip进行解压缩
* @param compressedStr
* @return
*/
public static byte[] uncompress(byte[] compressedStr) {
if (compressedStr == null) {
return null;
}
byte[] decompressed = null;
try {

ByteArrayInputStream in = new ByteArrayInputStream(compressedStr);
GZIPInputStream ginzip = new GZIPInputStream(in);


ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int offset = -1;
while ((offset = ginzip.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, offset);
}
decompressed = out.toByteArray();
in.close();
ginzip.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return decompressed;
}
}

 

这样就可以获取到BASE64解码并解压后的报文了,一般在用 JSON 报文格式请求接口时,有可能会出现这样的情况.

接口开发遇到BASE64编码并且报文被压缩的情况怎么办

标签:server   UNC   utf-8   zip   set   cep   array   cache   sed   

原文地址:https://www.cnblogs.com/kokimiki/p/13362620.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!