防止 Tomcat 将字符集附加到二进制内容类型
我们有一个基于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有某个位置时,servlet 容器才会执行此操作
仅当代码中 。另请参阅 javadoc< /a>.设置字符编码对于二进制数据没有意义,因此不要对二进制数据的请求执行此操作。
The servletcontainer does that only when you have a
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.
就我而言,它是 Spring 的
CharacterEncodingFilter
。如果将forceEncoding
设置为true
,它会将字符编码添加到内容类型中,即使对于二进制数据来说它没有任何意义。要修复此问题,请将
forceEncoding
设置为false
或保留默认设置。并验证它的效果,例如对 JSON 响应的效果。In my case, it turned out to be Spring's
CharacterEncodingFilter
. If you setforceEncoding
totrue
, 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
tofalse
or leave it in the default setting. And verify the effect it has, e.g. on JSON responses.在此阶段,我无法阻止 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.