博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
将响应数据进行压缩处理的过滤器(CompressionFilter)
阅读量:5459 次
发布时间:2019-06-15

本文共 4584 字,大约阅读时间需要 15 分钟。

    本系列文章导读:

        

        

        

        

       

功能描述

        如果浏览器支持 gzip 压缩格式的数据,则将响应的数据使用 gzip 压缩后再输出。

使用方法

        在 java web 项目的 web.xml 文件中添加如下代码。

CompressionFilter
com.hmw.filter.CompressionFilter
CompressionFilter
/LongServlet

过滤器源码

CompressionFilter.java

package com.hmw.filter;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.util.zip.GZIPOutputStream;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * 压缩过滤器  * 如果浏览器支持 gzip 压缩格式的数据,则将响应的数据使用 gzip 压缩后再输出。 *  * @author  */public class CompressionFilter implements Filter {    @Override    public void init(FilterConfig config) throws ServletException {    }    /**     * 如果浏览器不支持 gzip 压缩,则不做直接放行(不做压缩处理)      * 反之,将HTTP响应头的编码设置为 gzip,然后将响应数据使用 gzip 进行压缩处理。     */    @Override    public void doFilter(ServletRequest request, ServletResponse response,            FilterChain chain) throws ServletException, IOException {        HttpServletRequest req = (HttpServletRequest) request;        HttpServletResponse res = (HttpServletResponse) response;        if (!isGzipSupported(req)) { // Invoke resource normally.            chain.doFilter(req, res);            return;        }        // 将响应头信息中的内容编码设置为 gzip        res.setHeader("Content-Encoding", "gzip");                // 调用资源,使用 CharArrayWrapper 包装输出        CharArrayWrapper responseWrapper = new CharArrayWrapper(res);        chain.doFilter(req, responseWrapper);        // 取得存放输出数据的 char 型数组        char[] responseChars = responseWrapper.toCharArray();                // 将响应数据压缩后存入一个 byte 型的数组,然后输出到        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();        GZIPOutputStream zipOut = new GZIPOutputStream(byteStream);        OutputStreamWriter tempOut = new OutputStreamWriter(zipOut);        // 将原来的响应数据压缩后写入二字节输出流        tempOut.write(responseChars);        // 关闭输出流        tempOut.close();        // 更新响应头信息中 Content-Length 的值。        res.setContentLength(byteStream.size());        // 将压缩后的数据发送至客户端        OutputStream realOut = res.getOutputStream();        byteStream.writeTo(realOut);    }    @Override    public void destroy() {    }    /**     * 检测浏览器是否支持 Gzip 压缩     *      * @param req HTTP 请求对象     * @return 如果浏览器支持 Gzip 压缩,则返回 true,反之,则返回 false     */    private boolean isGzipSupported(HttpServletRequest req) {        String browserEncodings = req.getHeader("Accept-Encoding");        return ((browserEncodings != null) && (browserEncodings.indexOf("gzip") != -1));    }}

CharArrayWrapper.java

package com.hmw.filter;import java.io.CharArrayWriter;import java.io.PrintWriter;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpServletResponseWrapper;/** * A response wrapper that takes everything the client would normally output and * saves it in one big character array. */public class CharArrayWrapper extends HttpServletResponseWrapper {	private CharArrayWriter charWriter;	/**	 * Initializes wrapper.	 * 

* First, this constructor calls the parent constructor. That call is * crucial so that the response is stored and thus setHeader, *setStatus, * addCookie, and so forth work normally. *

* Second, this constructor creates a CharArrayWriter that will be used to * accumulate the response. */ public CharArrayWrapper(HttpServletResponse response) { super(response); charWriter = new CharArrayWriter(); } /** * When servlets or JSP pages ask for the Writer, don't give them the real * one. Instead, give them a version that writes into the character array. * The filter needs to send the contents of the array to the client (perhaps * after modifying it). */ @Override public PrintWriter getWriter() { return new PrintWriter(charWriter); } /** * Get a String representation of the entire buffer. *

* Be sure not to call this method multiple times on the same * wrapper. The API for CharArrayWriter does not guarantee that it * "remembers" the previous value, so the call is likely to make a new * String every time. */ @Override public String toString() { return charWriter.toString(); } /** Get the underlying character array. */ public char[] toCharArray() { return charWriter.toCharArray(); }}

转载于:https://www.cnblogs.com/hemingwang0902/archive/2012/01/12/compression-filter.html

你可能感兴趣的文章
PHP变量测试函数
查看>>
第六天 基本文件管理与XFS文件系统备份恢复
查看>>
win10中强制vs2015使用管理员启动
查看>>
UISerachBar / UISearchDisplayController
查看>>
Linux常用的操作命令
查看>>
Redis Desktop Manager
查看>>
css书写规范
查看>>
Asp.net +Jquery-uploadify多文件上传
查看>>
【恐怖的数组模拟】Secret Poems - HihoCoder - 1632
查看>>
大规模机器学习
查看>>
EasyPlayerPro(Windows)流媒体播放器开发之接口设计
查看>>
寻找数组中子数组和的最大值
查看>>
如何系统的进入大数据领域,学习路线是什么?
查看>>
COLLATE Chinese_PRC_CI_AS
查看>>
PHP中面对过程的冗余是什么?
查看>>
函数----函数重载,特殊用途语言特性,函数匹配,函数指针
查看>>
Hive数据查询
查看>>
在vue2框架中,使用背景图片时,在build压缩代码环节图片找不到路径
查看>>
[转]android中最好的瀑布流控件PinterestLikeAdapterView
查看>>
算法面经之百度
查看>>