gpt4 book ai didi

java - Jetty 8 GzipFilter 有时不适用

转载 作者:行者123 更新时间:2023-11-30 11:51:22 25 4
gpt4 key购买 nike

我刚刚将我的 Web 服务器从 Jetty 6.x 更新到 Jetty 8.0.1,出于某种原因,当我执行完全相同的请求时,响应有时被 Gzip 压缩,有时则没有。

这是请求和响应在 servlet 的 service() 方法开始时的样子:

Request: [GET /test/hello_world?param=test]@11538114 org.eclipse.jetty.server.Request@b00ec2
Response: org.eclipse.jetty.servlets.GzipFilter$2@1220fd1
WORKED!

Request:[GET /test/hello_world?param=test]@19386718 org.eclipse.jetty.server.Request@127d15e
Response:HTTP/1.1 200
Connection: close
FAILED!

这是我的 GzipFilter 声明:

EnumSet<DispatcherType> all = EnumSet.of(DispatcherType.ASYNC, DispatcherType.ERROR, DispatcherType.FORWARD,
DispatcherType.INCLUDE, DispatcherType.REQUEST);
FilterHolder gzipFilter = new FilterHolder(new GzipFilter());
gzipFilter.setInitParameter("mimeTypes", "text/javascript");
gzipFilter.setInitParameter("minGzipSize", "0");
context.addFilter(gzipFilter, "/test/*", all);

Javadoc 说:

GZIP Filter This filter will gzip the content of a response if:
The filter is mapped to a matching path ==>
The response status code is >=200 and <300
The content length is unknown or more than the minGzipSize initParameter or the minGzipSize is 0(default)
The content-type is in the comma separated list of mimeTypes set in the mimeTypes initParameter or if no mimeTypes are defined the content-type is not "application/gzip"
No content-encoding is specified by the resource

在我看来,所有这些条件都符合我的情况,除了最后一个“资源未指定内容编码”。我该如何验证?

另外,出于我也忽略的原因,当未使用 GzipFilter 过滤响应时,response.getWriter() 会抛出 IO 异常。这是为什么?

最佳答案

我遇到了同样的问题,找到了两个原因:

  1. 如果您的任何 servlet 过早地调用 response.getWriter().flush(),GZipFilter 将无法工作。在我的例子中,Spring MVC 和 Sitemesh 中的 Freemarker 都在这样做,所以我必须将 Freemarker servlet 和 Spring MVC Freemarker 配置对象的 Freemarker 设置“auto_flush”设置为“false”。
  2. 如果您使用普通的 GzipFilter,出于某种原因,它不允许您在 Jetty 的“包含”阶段设置任何 header 。所以我不得不使用 IncludableGzipFilter相反。

在我进行这些更改后,它对我有用。希望对您也有帮助。

关于java - Jetty 8 GzipFilter 有时不适用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7513922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com