首页 > 其他 > 详细

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

时间:2020-07-22 20:24:40      阅读:47      评论:0      收藏:0      [点我收藏+]

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编码并且报文被压缩的情况怎么办

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!