防止 Tomcat 将字符集附加到二进制内容类型

发布于 2024-11-07 00:14:57 字数 610 浏览 0 评论 0原文

我们有一个基于 Restlet 的服务,它返回以下响应:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1, Restlet-Framework/2.0.7
X-Powered-By: Servlet 2.4; JBoss-4.2.3.GA (build: SVNTag=JBoss_4_2_3_GA date=200807181417)/JBossWeb-2.0
Content-Disposition: inline; filename=Time_for_a_breather.pdf
Date: Fri, 13 May 2011 23:41:24 GMT
Accept-Ranges: bytes
Content-Type: application/pdf;charset=UTF-8
Content-Length: 218495

但不幸的是,在某些浏览器(特别是 Chrome)中,我们在查看 pdf 时遇到问题。

从实验和研究来看,问题似乎与 jBoss/Tomcat 将 charset=UTF-8 附加到内容类型有关,这导致浏览器感觉到它接收的是文本数据而不是二进制数据。

有谁知道阻止 jBoss/Tomcat 将字符集附加到二进制数据的内容类型的方法吗?

We have a Restlet based service that returns the following response:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1, Restlet-Framework/2.0.7
X-Powered-By: Servlet 2.4; JBoss-4.2.3.GA (build: SVNTag=JBoss_4_2_3_GA date=200807181417)/JBossWeb-2.0
Content-Disposition: inline; filename=Time_for_a_breather.pdf
Date: Fri, 13 May 2011 23:41:24 GMT
Accept-Ranges: bytes
Content-Type: application/pdf;charset=UTF-8
Content-Length: 218495

but unfortunately within some browsers (Chrome in particular) we are having problems viewing the pdf.

From experimenation and research it appears the problem is related to jBoss/Tomcat appending charset=UTF-8 to the Content Type which causes the browser to sense that its receiving text data not binary data.

Does anybody know of a way from preventing jBoss/Tomcat appending the charset to the content type for binary data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等风也等你 2024-11-14 00:14:57

有某个位置时,servlet 容器才会执行此操作

response.setCharacterEncoding("UTF-8");

仅当代码中 。另请参阅 javadoc< /a>.设置字符编码对于二进制数据没有意义,因此不要对二进制数据的请求执行此操作。

The servletcontainer does that only when you have a

response.setCharacterEncoding("UTF-8");

somewhere in your code. See also the javadoc. Setting the character encoding makes no sense for binary data, so just don't do that on requests for binary data.

枕梦 2024-11-14 00:14:57

就我而言,它是 Spring 的 CharacterEncodingFilter。如果将 forceEncoding 设置为 true,它会将字符编码添加到内容类型中,即使对于二进制数据来说它没有任何意义。

要修复此问题,请将 forceEncoding 设置为 false 或保留默认设置。并验证它的效果,例如对 JSON 响应的效果。

In my case, it turned out to be Spring's CharacterEncodingFilter. If you set forceEncoding to true, it will add the character encoding to the content type, even if it makes no sense as for binary data.

In order to fix it, set the forceEncoding to false or leave it in the default setting. And verify the effect it has, e.g. on JSON responses.

谎言月老 2024-11-14 00:14:57

在此阶段,我无法阻止 Tomcat 将 charset=UTF-8 附加到 Content-Type 标头,这导致 Chrome PDFViewer(内部 PDF 查看器)无法查看 PDF 文档。

在实验过程中,我发现如果我不返回 Content-Length 并设置 Transfer-Encoding=chunked,那么 Chrome 的 PDFViewer 就可以查看 PDF。这是目前的一个解决方法,但可能是一个脆弱的解决方案。

At this stage I have been unable to stop Tomcat from appending charset=UTF-8 to the Content-Type header which is causing Chrome PDFViewer (internal PDF viewer) to fail viewing the PDF document.

While experimenting I have discovered that if I don't return the Content-Length and set Transfer-Encoding=chunked then the PDF is viewable by Chrome's PDFViewer. This is a workaround for the moment but probably a fragile solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文