gzip压缩

两盒软妹~` 提交于 2019-12-19 02:55:34

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();
}
}
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!