首页 > 其他 > 详细

gzip压缩

时间:2019-12-02 16:21:09      阅读:75      评论:0      收藏:0      [点我收藏+]

public class GzipDemo{
public static void main(String[] args) throws IOException {
JSONObject request = new JSONObject();
String originStr = JSON.toJSONString(request);
byte[] dess = compressToByte(originStr);
ByteArrayEntity byteArrayEntity = new ByteArrayEntity(dess);
byteArrayEntity.setContentEncoding("gzip");
byteArrayEntity.setContentType("application/json");
HttpUriRequest gzip = RequestBuilder.post("url")
.setEntity(byteArrayEntity)
.setHeader("xxx-content","gzip")
.build();
JSONObject post = LocalHttpClient.executeJsonResult(gzip);
System.out.println(JSON.toJSONString(post));
}

public static byte[] compressToByte(String src) throws IOException {
if (StringUtils.isBlank(src)) {
throw new RuntimeException("GZipUtil.compressToByte error,params is blank");
} else {
ByteArrayOutputStream out = new ByteArrayOutputStream();

try {
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(src.getBytes("UTF-8"));
gzip.close();
} catch (IOException var4) {
throw new RuntimeException("GZipUtil.compressToByte error", var4);
}

return out.toByteArray();
}
}
}

gzip压缩

原文:https://www.cnblogs.com/3xiaoleilei/p/11970548.html

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