為了減少資料在網路中的傳輸量,從而減少傳輸時長,增加用戶體驗,瀏覽器大都是支持Gzip壓縮技術的,http的請求頭 Accept-Encoding:gzip, deflate 就表示這次請求可以接受Gzip壓縮后的資料,圖片不要進行壓縮,因為圖片完全可以在專案開發中使用壓縮后的圖片,壓碩訓有一定的CPU性能損耗,
下面介紹幾種 Gzip壓縮方式
1、SpringBoot開啟Gzip壓縮
在application.properties中加入如下配置:
server:
compression:
enabled: true
mime-types: application/json,application/xml,text/html,text/xml,text/plain,text/javascript,text/css,application/javascript
min-response-size: 256

壓縮后檔案大概有5-8倍左右的差距,能大大減少網路傳輸量,頁面加載速度加快
2、Tomcat開啟Gzip壓縮
tomcat中使用gzip需要進行配置,在server.xml中,在Connector標簽中加入如下屬性
compression="on"
compressionMinSize="2048"
compressableMimeType="text/html,text/css,text/javascript"
3、GZIPOutputStream,GZIPInputStream壓縮與解壓
package com.example.demo.controller;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import org.apache.tomcat.util.codec.binary.StringUtils;
public class GZIPUtils {
public static final String GZIP_ENCODE_UTF_8 = "UTF-8";
public static final String GZIP_ENCODE_ISO_8859_1 = "ISO-8859-1";
/**
* 字串壓縮為GZIP位元組陣列
* @param str
* @return
*/
public static byte[] compress(String str) {
return compress(str, GZIP_ENCODE_UTF_8);
}
/**
* 字串壓縮為GZIP位元組陣列
* @param str
* @param encoding
* @return
*/
public static byte[] compress(String str, String encoding) {
if (str == null || str.length() == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip;
try {
gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes(encoding));
gzip.close();
} catch (IOException e) {
e.printStackTrace();
}
return out.toByteArray();
}
/**
* GZIP解壓縮
* @param bytes
* @return
*/
public static byte[] uncompress(byte[] bytes) {
if (bytes == null || bytes.length == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
try {
GZIPInputStream ungzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = ungzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
} catch (IOException e) {
e.printStackTrace();
}
return out.toByteArray();
}
/**
* 解壓并回傳String
* @param bytes
* @return
*/
public static String uncompressToString(byte[] bytes) {
return uncompressToString(bytes, GZIP_ENCODE_UTF_8);
}
/**
* 解壓
* @param bytes
* @param encoding
* @return
*/
public static String uncompressToString(byte[] bytes, String encoding) {
if (bytes == null || bytes.length == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
try {
GZIPInputStream ungzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = ungzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
return out.toString(encoding);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String str = "%5B%7B%22lastUpdateTime%22%3A%222011-10-28+9%3A39%3A41%22%2C%22smsList%22%3A%5B%7B%22liveState%22%3A%221";
System.out.println("壓縮后字串:" + GZIPUtils.compress(str).toString());
System.out.println("原長度:" + str.length());
System.out.println("壓縮后字串長度:" + GZIPUtils.compress(str).toString().length());
System.out.println("解壓縮后字串:" + StringUtils.newStringUtf8(GZIPUtils.uncompress(GZIPUtils.compress(str))));
System.out.println("解壓縮后字串:" + GZIPUtils.uncompressToString(GZIPUtils.compress(str)));
}
}
下面我們對比一下沒有開啟gzip壓縮和開啟gzip壓縮的傳輸量
1,未開啟gzip壓縮的截圖,可以看到json大小為7.0MB

Request Headers里面可以看到前端已經支持了接受gzip壓縮后的json

2,開啟gzip壓縮后的截圖,可以看到json的大小壓縮成了1.2MB 大概壓縮了5.8倍

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/351260.html
標籤:其他
上一篇:選擇從最重到最輕的包裹
