为了减少数据在网络中的传输量,从而减少传输时长,增加用户体验,浏览器大都是支持Gzip压缩技术的,http的请求头 Accept-Encoding:gzip, deflate 就表示这次请求可以接受Gzip压缩后的数据,图片不要进行压缩,因为图片完全可以在项目开发中使用压缩后的图片。压缩会有一定的CPU性能损耗。
下面介绍几种 Gzip压缩方式
1.SpringBoot开启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
或者在application.yml 中加入如下配置:
server:
compression:
enabled: true
mime-types: application/javascript,text/css,application/json,application/xml,text/html,text/xml,text/plain
压缩后文件大概有5-8倍左右的差距,能大大减少网络传输量,页面加载速度加快
2.Tomcat开启Gzip压缩
tomcat中使用gzip需要进行配置,在server.xml中,在Connector标签中加入如下属性
compression="on"
compressionMinSize="2048"
compressableMimeType="text/html,text/css,text/javascript"
原文:https://www.cnblogs.com/xiaohanlin/p/12218213.html