问题描述
MIME类型在Tomcat的conf/web.xml文件中指定.看起来像这样:
Mime-types are specified in Tomcat's conf/web.xml file.It's look like this:
<mime-mapping>
<extension>txt</extension>
<mime-type>text/plain</mime-type>
</mime-mapping>
以前,我尝试以下操作:
Previously I try following:
<mime-mapping>
<extension>*</extension>
<mime-type>application/octet-stream</mime-type>
</mime-mapping>
但这对我没有帮助.如何为任何文件扩展名指定默认的mime类型?
but it doesn't help me.How to specify default mime-type for any file extension?
推荐答案
没有办法.您需要自己明确设置它们. servlet过滤器是适合此的地方.
There's no way. You need to explicitly set them yourself. A servlet filter is a suitable place for this.
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
response.setContentType("application/octet-stream");
chain.doFilter(request, response);
}
但是,我高度质疑企业对此的需求.这对于SEO和客户来说都是不利的.如果您的唯一目的是弹出另存为对话框,那么您应该这样说.有比实现错误的mime类型更好的解决方案.
I however highly question the business need for this. It's only disadvantageous for SEO and the client. If your sole purpose is to pop a Save As dialogue, then you should say that so. There are much better solutions to achieve this than forcing a wrong mime type.
这篇关于如何在Tomcat 6中为任何文件扩展名设置默认的mime-tipe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!