Spring使用GZIP返回文本数据

要求在Spring项目中使用GZIP压缩来减少网络数据传输量:

结果如下: 压缩前: image.png

压缩后: image.png

压缩前:249K数据

压缩后:38K数据

基本缩小了接近到6分之一,可见对文本数据的压缩量还是比较大的;

代码:

ResponseEntity responseEntity = ...;
byte[] gzipData = gzip(JSONObject.toJSONString(responseEntity).getBytes());
logger.info("===== " + JSONObject.toJSONString(responseEntity).getBytes().length + " => " + gzipData.length);
response.addHeader("Content-Encoding", "gzip");
response.setContentLength(gzipData.length);
try {
     ServletOutputStream output = response.getOutputStream();
     output.write(gzipData);
     output.flush();
} catch (IOException e) {
     e.printStackTrace();
} finally {
}
private byte[] gzip(byte[] data) {
        ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
        GZIPOutputStream output = null;
        try {
            output = new GZIPOutputStream(byteOutput);
            output.write(data);
        } catch (IOException e) {
        } finally {
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return byteOutput.toByteArray();
    }

获取GZIP数据并且解压

public static String sendPostForGzipString(String url, String body, Map headers) {
        PrintWriter out = null;
        BufferedReader bufferReader = null;
        StringBuilder stringBuilder = new StringBuilder();

        try {
            URL realUrl = new URL(url);
            URLConnection conn = realUrl.openConnection();

            if (headers != null) {
                Iterator var9 = headers.entrySet().iterator();

                while (var9.hasNext()) {
                    Map.Entry header = (Map.Entry) var9.next();
                    conn.setRequestProperty((String) header.getKey(), (String) header.getValue());
                }
            }

            conn.setRequestProperty(“Method”, “POST”);
            conn.setRequestProperty(“Content-Type”, “application/json”);
            conn.setRequestProperty(“Charset”, “UTF-8”);
            conn.setRequestProperty(“connection”, “Keep-Alive”);
            conn.setRequestProperty(“Accept-Encoding”, “gzip”);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            out = new PrintWriter(conn.getOutputStream());
            out.print(body);
            out.flush();
            GZIPInputStream gzin = new GZIPInputStream(conn.getInputStream());
            bufferReader = new BufferedReader(new InputStreamReader(gzin));
            String line = null;

            while ((line = bufferReader.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (Exception var17) {
        } finally {
            try {
                if (out != null) {
                    out.close();
                }

                if (bufferReader != null) {
                    bufferReader.close();
                }
            } catch (IOException var16) {
            }
        }
        return stringBuilder.toString();
    }
# Spring 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×