定义GzipFilter对输出进行拦截GzipFilter 拦截器获取缓存的需要输出的数据,进行压缩,在输出数据之前先设置响应头Content-Encoding : gzip
。
package com.olive.filter;import lombok.extern.slf4j.Slf4j;import org.springframework.http.HttpHeaders;import javax.servlet.*;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.*;import java.util.zip.GZIPOutputStream;/** * 压缩过滤器 * * 功能:对于返回给客户端的数据进行gzip压缩,提高响应速度 * 实现说明: *要对response对象的输出数据进行gzip压缩,首先得拿到后面servlet(controller)进行业务处理后往response对象里写入的数据 *可以通过重写response对象,修改该对象内部的输出流,使该流写出数据时写出到给定的字节数组缓冲流当中, *并在重写后的response对象内部提供一个获取该字节数组缓冲流的方法,这样就可以截获响应数据 *然后就可以对截获的响应数据通过Gzip输出流进行压缩输出即可; *因为响应数据是gzip压缩格式,不是普通的文本格式所以需要通过response对象(响应头)告知浏览器响应的数据类型 */@Slf4jpublic class GzipFilter implements Filter {private final String GZIP = "gzip";public void destroy() {log.info("GzipFilter destroy");}public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {log.info("GzipFilter start");HttpServletRequest request = (HttpServletRequest) req;HttpServletResponse response = (HttpServletResponse) resp;String acceptEncoding = request.getHeader(HttpHeaders.ACCEPT_ENCODING);//searching for 'gzip' in ACCEPT_ENCODING headerif( acceptEncoding != null && acceptEncoding.indexOf(GZIP) >= 0){GzipResponseWrapper gzipResponseWrapper = new GzipResponseWrapper(response);//pass the customized response object to controller to capture the output datachain.doFilter(request, gzipResponseWrapper);//get captured databyte[] data = https://www.huyubaike.com/biancheng/gzipResponseWrapper.getOutputData();log.info("截获到数据:" + data.length + " bytes");//get gzip dataByteArrayOutputStream gzipBuffer = new ByteArrayOutputStream();GZIPOutputStream gzipOut = new GZIPOutputStream(gzipBuffer);gzipOut.write(data);gzipOut.flush();gzipOut.close();byte[] gzipData = https://www.huyubaike.com/biancheng/gzipBuffer.toByteArray();log.info("压缩后数据:" + gzipData.length + " bytes");//set response header and outputresponse.setHeader(HttpHeaders.CONTENT_ENCODING, GZIP);response.getOutputStream().write(gzipData);response.getOutputStream().flush();}else{chain.doFilter(req, resp);}}public void init(FilterConfig config) throws ServletException {log.info("GzipFilter init");}}
注册 GzipFilter 拦截器package com.olive.config;import com.olive.filter.GzipFilter;import org.springframework.boot.web.servlet.FilterRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * 注册filter */@Configurationpublic class FilterRegistration {@Beanpublic FilterRegistrationBean<GzipFilter> gzipFilterRegistrationBean() {FilterRegistrationBean<GzipFilter> registration = new FilterRegistrationBean<>();//Filter可以new,也可以使用依赖注入Beanregistration.setFilter(new GzipFilter());//过滤器名称registration.setName("gzipFilter");//拦截路径registration.addUrlPatterns("/*");//设置顺序registration.setOrder(1);return registration;}}
定义 Controller该 Controller 非常简单,主要读取一个大文本文件,作为输出的内容 。
package com.olive.controller;import java.io.File;import java.util.HashMap;import java.util.Map;import com.olive.vo.ArticleRequestVO;import org.apache.commons.io.FileUtils;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class TestController { @RequestMapping("/getArticle") public Map<String, Object> getArticle(){Map<String, Object> result = new HashMap<>();result.put("code", 200);result.put("msg", "success");byte[] bytes = null;try {bytes = FileUtils.readFileToByteArray(new File("C:\\Users\\2230\\Desktop\\凯平项目资料\\改装车项目\\CXSSBOOT_DB_DDL-1.0.9.sql"));}catch (Exception e){}String content = new String(bytes);ArticleRequestVO vo = new ArticleRequestVO();vo.setId(1L);vo.setTitle("BUG弄潮儿");vo.setContent(content);result.put("body", vo);return result; }}
经验总结扩展阅读
- SpringBoot+Vue3 AgileBoot - 手把手一步一步带你Run起全栈项目
- 三伏天如何减肥快
- 咸鱼之王龙鱼坚盾怎么搭配
- 宝石研物语:伊恩之石好感度角色是什么
- 宝石研物语:伊恩之石迈向绚烂之时活动在哪
- 宝石研物语:伊恩之石迈向绚烂之时最后一关怎么打
- 宝石研物语:伊恩之石怎么培养莉莉的好感度
- 适合网恋不适合奔现的星座
- 原神熏风之境怎么通关
- 宝石研物语:伊恩之石都有哪些称号