為了減少數(shù)據(jù)在網(wǎng)絡(luò)中的傳輸量,從而減少傳輸時(shí)長(zhǎng)复濒,增加用戶體驗(yàn)脖卖,瀏覽器大都是支持Gzip壓縮技術(shù)的,http的請(qǐng)求頭 Accept-Encoding:gzip, deflate 就表示這次請(qǐng)求可以接受Gzip壓縮后的數(shù)據(jù)巧颈,圖片不要進(jìn)行壓縮畦木,因?yàn)閳D片完全可以在項(xiàng)目開(kāi)發(fā)中使用壓縮后的圖片。壓縮會(huì)有一定的CPU性能損耗砸泛。
下面介紹幾種 Gzip壓縮方式
1.SpringBoot開(kāi)啟Gzip壓縮
在application.properties中加入如下配置:
server.compression.enabled=true
server.compression.mime-types=application/javascript,text/css,application/json,application/xml,text/html,text/xml,text/plain
壓縮前:25.3kb十籍,50.0kb,37.5kb唇礁,5.1kb勾栗,34.7kb
壓縮后:6.4kb,11.7kb盏筐,8.3kb围俘,1.3kb,34.7kb
壓縮后可看到文件有4倍左右的差距琢融,能大大減少網(wǎng)絡(luò)傳輸量界牡,頁(yè)面加載速度加快
2.Tomcat開(kāi)啟Gzip壓縮
tomcat中使用gzip需要進(jìn)行配置,在server.xml中漾抬,在Connector標(biāo)簽中加入如下屬性
compression="on"
compressionMinSize="2048"
compressableMimeType="text/html,text/css,text/javascript"
3.Nginx開(kāi)啟Gzip壓縮
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary off;
重載nginx即可
第1行:開(kāi)啟Gzip
第2行:不壓縮臨界值宿亡,大于1K的才壓縮,一般不用改
第3行:buffer奋蔚,不用改
第4行:用了反向代理的話她混,末端通信是HTTP/1.0,有需求的應(yīng)該也不用看我這科普文了泊碑;有這句的話注釋了就行了坤按,默認(rèn)是HTTP/1.1
第5行:壓縮級(jí)別,1-10馒过,數(shù)字越大壓縮的越好臭脓,時(shí)間也越長(zhǎng),看心情隨便改吧
第6行:進(jìn)行壓縮的文件類型腹忽,缺啥補(bǔ)啥就行了来累,JavaScript有兩種寫(xiě)法砚作,最好都寫(xiě)上吧,總有人抱怨js文件沒(méi)有壓縮嘹锁,其實(shí)多寫(xiě)一種格式就行了
第7行:跟Squid等緩存服務(wù)有關(guān)葫录,on的話會(huì)在Header里增加”Vary: Accept-Encoding”,我不需要這玩意领猾,自己對(duì)照情況看著辦吧
4.GZIPOutputStream米同,GZIPInputStream壓縮與解壓
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.commons.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字節(jié)數(shù)組
* @param str
* @return
*/
public static byte[] compress(String str) {
return compress(str, GZIP_ENCODE_UTF_8);
}
/**
* 字符串壓縮為GZIP字節(jié)數(shù)組
* @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("原長(zhǎng)度:" + 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)));
}
}