浏览器何时以Content-Type的形式发送应用程序/八位字节流? [英] When do browsers send application/octet-stream as Content-Type?
问题描述
$ ul
$ b
我的问题是,一些文件保存在内容类型= <即使它们是 *。doc
文件或$
何时浏览器提交这样的内容类型?
我想清理数据库,所以我需要知道浏览器信息何时不正确。 解决方案
忽略浏览器发送的值。这确实取决于所使用的客户端平台,浏览器和配置。
如果您想基于文件扩展名完全控制内容类型,那么最好使用< a href =http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletContext.html#getMimeType%28java.lang.String%29 =nofollow noreferrer> 的ServletContext#getMimeType()
。
字符串mimeType = servletContext.getMimeType(filename);
默认的mime类型定义在 web.xml中$ c有问题的servletcontainer的$ c>。例如Tomcat,它位于
/conf/web.xml
中。您可以在webapp的 /WEB-INF/web.xml
中扩展/覆盖它,如下所示:
< MIME映射>
< extension> xlsx< / extension>
< mime-type> application / vnd.openxmlformats-officedocument.spreadsheetml.sheet< / mime-type>
< / mime-mapping>
您也可以根据实际文件内容确定MIME类型(因为文件扩展名可能不是每个准确,可以被客户所愚弄),但这是很多工作。考虑使用第三方库来完成所有的工作。我发现 JMimeMagic 对此很有用。您可以按如下方式使用它:
String mimeType = Magic.getMagicMatch(file,false).getMimeType();
请注意,它不支持所有 mimetypes。你也可以考虑两种方法的组合。例如。如果返回null或 application / octet-stream
,则使用另一个。或者,如果两者都返回一个不同的,但有效的MIME类型,更喜欢由JMimeMagic返回的。
哦,我几乎忘了添加,在JSF中,您可以获得 ServletContext
如下所示:
$ b $ $ pre $ ServletContext servletContext =(ServletContext)FacesContext.getCurrentInstance 。.getExternalContext()的getContext();
或者如果您碰巧已经使用JSF 2.x,请使用 ExternalContext#getMimeType( )
代替。
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.
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.
这篇关于浏览器何时以Content-Type的形式发送应用程序/八位字节流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!