为了减少数据在网络中的传输量,从而减少传输时长,增加用户体验,浏览器大都是支持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倍