Java deflate response

2019-07-20 11:15发布

问题:

Hello all I want to make a filter for tomcat to deflate all responces of certain MIME type. Any guidelines?

...
 String ae = request.getHeader("accept-encoding");
        if (ae != null && ae.indexOf("deflate") != -1) {
            deflate response...?????
        }
chain.doFilter(request, res);

回答1:

Don't do that in a homebrewed Filter. Configure it at server level. In case of for example Apache Tomcat, just add compression="on" to <Connector> element in /conf/server.xml. It will GZIP responses whenever client accepts it (GZIP is based on deflate and practically every client supports it whenever deflate is supported).

<Connector compression="on">

That's all. You can if necessary configure mime types by compressableMimeType attribute.

See also:

  • Apache Tomcat Configuration Reference - The HTTP Connector