解析SpringBoot项⽬开发之Gzip压缩过程
为了减少数据在⽹络中的传输量,从⽽减少传输时长,增加⽤户体验,浏览器⼤都是⽀持Gzip压缩技术的,http的请求头 Accept-Encoding:gzip, deflate 就表⽰这次请求可以接受Gzip压缩后的数据,图⽚不要进⾏压缩,因为图⽚完全可以在项⽬开发中使⽤压缩后的图⽚。压缩会有⼀定的CPU性能损耗。
下⾯介绍⼏种 Gzip压缩⽅式
1.SpringBoot开启Gzip压缩
在application.properties中加⼊如下配置:
serverpression.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倍左右的差距,能⼤⼤减少⽹络传输量,页⾯加载速度加快
2.Tomcat开启Gzip压缩
tomcat中使⽤gzip需要进⾏配置,在l中,在Connector标签中加⼊如下属性
compression="on"
compressionMinSize="2048"
compressableMimeType="text/html,text/css,text/javascript"
3.Nginx开启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⾏:开启Gzip
第2⾏:不压缩临界值,⼤于1K的才压缩,⼀般不⽤改
第3⾏:buffer,不⽤改
第4⾏:⽤了反向代理的话,末端通信是HTTP/1.0,有需求的应该也不⽤看我这科普⽂了;有这句的话注释了就⾏了,默认是HTTP/1.1
第5⾏:压缩级别,1-10,数字越⼤压缩的越好,时间也越长,看⼼情随便改吧
第6⾏:进⾏压缩的⽂件类型,缺啥补啥就⾏了,JavaScript有两种写法,最好都写上吧,总有⼈抱怨js⽂件没有压缩,其实多写⼀种格式就⾏了
第7⾏:跟Squid等缓存服务有关,on的话会在Header⾥增加"Vary: Accept-Encoding",我不需要这玩意,⾃⼰对照情况看着办吧
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 dec.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.Bytes(encoding));
gzip.close();
} catch (IOException e) {
e.printStackTrace();
}
ByteArray();
}
/**
* 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 = ad(buffer)) >= 0) {
out.write(buffer, 0, n);
}
} catch (IOException e) {
e.printStackTrace();
}
ByteArray();
}
/**
* 解压并返回String
* @param bytes
* @return
*/
springboot是啥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 = ad(buffer)) >= 0) {
out.write(buffer, 0, n);
}
String(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("原长度:" + str.length());
System.out.println("压缩后字符串:" + GZIPUtilspress(str).toString().length());
System.out.println("解压缩后字符串:" + wStringUtf8(GZIPUtils.uncompress(GZIPUtilspress(str))));
System.out.println("解压缩后字符串:" + GZIPUtils.uncompressToString(GZIPUtilspress(str)));
}
}
到此这篇关于SpringBoot项⽬开发之Gzip压缩过程的⽂章就介绍到这了,更多相关SpringBoot Gzip压缩内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。