浏览器何时将 application/octet-stream 作为 Content-Type 发送?

发布于 2024-08-24 14:25:53 字数 282 浏览 10 评论 0原文

我正在使用 JSF 开发文件上传。应用程序保存有关文件的三个日期:

  • 文件名
  • 字节
  • 内容类型 由浏览器提交。

我的问题是,某些文件以 content type = application/octet-stream 保存,即使它们是 *.doc 文件或 *.pdf

浏览器什么时候提交这样的内容类型?
我想清理数据库,所以我需要知道浏览器信息何时不正确。

I'm developing a file upload with JSF. The application saves three dates about the file:

  • Filename
  • Bytes
  • Content-Type as submitted by the browser.

My problem is that some files are saved with content type = application/octet-stream even if they are *.doc files oder *.pdf.

When does the browser submits such a content type?
I would like to clean up the database so I need to know when the browser information are incorrect.

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

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

发布评论

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

评论(2

瑶笙 2024-08-31 14:25:53

忽略浏览器发送的值。这确实取决于所使用的客户端平台、浏览器和配置。

如果您想根据文件扩展名完全控制内容类型,那么最好使用 ServletContext#getMimeType()

String mimeType = servletContext.getMimeType(filename);

默认 mime 类型在相关 servlet 容器的 web.xml 中定义。例如,在 Tomcat 中,它位于 /conf/web.xml 中。您可以在 web 应用程序的 /WEB-INF/web.xml 中扩展/覆盖它,如下所示:

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

您还可以根据实际文件内容确定 mime 类型(因为文件扩展名本身可能不是准确,可能会被客户愚弄),但这需要大量工作。考虑使用第三方库来完成所有工作。我发现 JMimeMagic 对此很有用。您可以按如下方式使用它:

String mimeType = Magic.getMagicMatch(file, false).getMimeType();

请注意,它不支持所有可靠的mime类型。您还可以考虑结合使用这两种方法。例如,如果其中一个返回 null 或 application/octet-stream,则使用另一个。或者,如果两者都返回不同但“有效”的 mimetype,则更喜欢 JMimeMagic 返回的类型。

哦,我差点忘了补充一下,在 JSF 中,您可以按如下方式获取 ServletContext

ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();

或者如果您碰巧已经使用 JSF 2.x,请使用 ExternalContext#getMimeType() 相反。

Ignore the value sent by the browser. This is indeed dependent on the client platform, browser and configuration used.

If you want full control over content types based on the file extension, then better determine it yourself using ServletContext#getMimeType().

String mimeType = servletContext.getMimeType(filename);

The default mime types are definied in the web.xml of the servletcontainer in question. In for example Tomcat, it's located in /conf/web.xml. You can extend/override it in the webapp's /WEB-INF/web.xml as follows:

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

You can also determine the mime type based on the actual file content (because the file extension may not per se be accurate, it can be fooled by the client), but this is a lot of work. Consider using a 3rd party library to do all the work. I've found JMimeMagic useful for this. You can use it as follows:

String mimeType = Magic.getMagicMatch(file, false).getMimeType();

Note that it doesn't support all mimetypes as reliable. You can also consider a combination of both approaches. E.g. if the one returns null or application/octet-stream, use the other. Or if both returns a different but "valid" mimetype, prefer the one returned by JMimeMagic.

Oh, I almost forgot to add, in JSF you can obtain the ServletContext as follows:

ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();

Or if you happen to use JSF 2.x already, use ExternalContext#getMimeType() instead.

琉璃梦幻 2024-08-31 14:25:53

这取决于操作系统、浏览器以及用户如何配置它们。它基于浏览器确定本地文件的文件类型(以显示它们)的方式。在大多数操作系统/浏览器组合上,这基于文件的扩展名,但在某些操作系统/浏览器组合上,它可能通过其他方式确定。 (例如:在 Mac OS 上)

在任何情况下,您都不应该真正依赖浏览器发送的内容类型。最好的方法是实际查看文件的内容。您也可以使用文件名,但请记住,浏览器不一定会很好地告诉您(尽管它可能仍然比它们发送的内容类型可靠很多) )。

It depends on the OS, the browser, and how the user has configured them. It's based on the way the browser determines the file type of local files (to display them). On most OS/browser combinations this is based on the file's extension, but on some it may be determined by other means. (eg: on Mac OS)

In ay case, you shouldn't really rely on the Content-type sent by the browser. The best approach would be to actually look at the contents of the file. You could probably also use the filename, but keep in mind that browsers aren't necessarily going to be good about telling you that either (though it's probably still a lot more reliable than the Content-type they send).

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