如何指定文档的响应内容类型以便跨浏览器一致地工作? [英] How should response content type for documents be specified in order to work consistently across browsers?

查看:125
本文介绍了如何指定文档的响应内容类型以便跨浏览器一致地工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应该通过http提供文档的简单servlet。我使用/ getDocument形式的URL?fileId = 1234。 servlet简单地1)设置response.contentType和2)写入response.outputStream。

I'm developing a simple servlet that should serve documents via http. I use URLs in the form of /getDocument?fileId=1234. The servlet simply 1) sets response.contentType and 2) writes to response.outputStream.

问题是关于正确设置内容类型以使浏览器理解响应(即,使用正确的应用程序显示文档)。具体来说:

The problem is about setting the content type correctly in order to have the browsers understand the response (i.e., display the document using the correct application). Specifically:

a)对于PDF文件,如果我将内容类型设置为application / pdf,Internet Explorer会理解(立即显示文档)但Firefox不会(显示空白页面而不试图打开任何pdf查看器插件)。如果我将它设置为application / x-octetstream,Firefox会理解它(正确显示),但是当它要求我保存或打开它时,Internet Explorer会说未知文件类型。

a) For PDF files, if I set content type to "application/pdf", Internet Explorer understands (displays the document immediately) but Firefox does not (displays blank page without attempting to open any pdf viewer plugin). If I set it to "application/x-octetstream", Firefox understands it (displays it properly), but Internet Explorer says "unknown file type" when it asks me to save or open it.

b)Firefox了解application / msword和application / vnd.ms-excel,但奇怪的是,Internet Explorer没有,它简单地说未知文件类型。

b) Firefox understands "application/msword" and "application/vnd.ms-excel", but Internet Explorer does, strangely, not, it simple says "unknown file type".

是否有可能在所有浏览器中始终如一地工作,如果是这样,为各种文档类型设置内容类型的正确方法是什么?是否还有其他在响应中设置以便正常工作?或者,正如我怀疑的那样,当URL没有以相应的文件扩展名结束时,浏览器是否会感到困惑? (即,getFile?fileId = 1234而不是例如getFile / test.pdf)

Is it possible to get this to work consistently in all browsers, and if so, what is the correct way of setting content type for various document types? Is there anything else that should be set in the response in order for this do work correctly? Or, as I suspect, are the browsers getting confused when the URL does not end with the corresponding filename extension? (i.e., getFile?fileId=1234 instead of e.g. getFile/test.pdf)

推荐答案

在servlet内部,响应内容类型应设置如下:

Inside a servlet, the response content type ought to be set as follows:

response.setContentType(getServletContext().getMimeType(filenameWithExtension));

ServletContext#getMimeType() 查找与某些文件扩展名关联的内容类型的 web.xml 中的所有< mime-mapping> 条目。您可以在appserver自己的 web.xml 中找到所有默认映射(例如,如果Tomcat位于 /conf/web.xml )。它可能缺少新的MSOffice OpenXML文件扩展名,如 xlsx docx 等等。您可以将它们添加到您的webapp的 web.xml ,如下所示:

The ServletContext#getMimeType() lookups all <mime-mapping> entries in web.xml for the content types associated with certain file extensions. You can find all default mappings in the appserver's own web.xml (which in case of e.g. Tomcat is located in /conf/web.xml). It might lack the "new" MSOffice OpenXML file extensions like xlsx, docx and so on. You can add them to your webapp's web.xml like follows:

<mime-mapping>
    <extension>xlsx</extension>
    <mime-type>application/vnd.openxmlformats-officedocument.spreadsheetml.sheet</mime-type>
</mime-mapping>

关于浏览器如何处理内容类型和相关应用程序,这里的主要问题是MSIE。它忽略 Content-Type 标题和 Content-Disposition的 filename 参数标题。它改为 smartass-ingly 根据URL中的文件扩展名猜测内容类型,并使用URL的最后一个pathinfo作为文件名。由于您使用了诸如 / getDocument?fileId = 1234 之类的请求参数而不是一个完整的文件名+扩展名,因此文件名将变为 getDocument 和MSIE无法可靠地猜测它的mime类型。你应该在URL中包含文件名+扩展名,如 /getDocument/filename.ext 。您可以通过 request.getPathInfo() Servlet 中获取该部分。有关更多servlet提示,请参阅本文

As to how browsers handle the content type and the associated application, the major problem here is MSIE. It ignores the Content-Type header and the filename parameter of the Content-Disposition header. It instead smartass-ingly guesses the content type based on the file extension in the URL and uses the last pathinfo of the URL as filename. As you've used a request parameter like /getDocument?fileId=1234 instead of a fullworthy filename+extension, the filename will become getDocument and MSIE can't reliability "guess" the mime type of it. You should really include the filename+extension in the URL like /getDocument/filename.ext. You can grab that part in the Servlet by request.getPathInfo(). For more servlet hints also see this article.

关于Firefox无法正确处理PDF文件的问题,这必须是Firefox中的错误配置。尝试在工具> 选项> 应用程序中验证所有内容是否正常。即应该以正确的方式尊重上述标题。您应该只确保指定了正确(!!)的任何 Content-Length 标头,否则无法打开文件。

As to the problem of your Firefox not handling PDF files correctly, this must be a misconfiguration in your Firefox. Try verifying if everything looks right in Tools > Options > Applications. It is namely supposed to respect the aforementioned headers in a correct manner. You should only ensure that any Content-Length header is correctly(!!) specified, else the file cannot be opened.

这篇关于如何指定文档的响应内容类型以便跨浏览器一致地工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆