gzip压缩

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

posted @ 2019-12-02 14:50  海尚书  阅读(292)  评论(0编辑  收藏  举报