Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default mime-tipe for any file extension in Tomcat 6?

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> 

but it doesn't help me. How to specify default mime-type for any file extension?

like image 496
Ivan Kaplin Avatar asked Jul 19 '10 16:07

Ivan Kaplin


People also ask

What is the default MIME type?

Two primary MIME types are important for the role of default types: text/plain is the default value for textual files. A textual file should be human-readable and must not contain binary data. application/octet-stream is the default value for all other cases.

What is MIME type for all files?

Use "application/octet-stream" for the "All files (*)" filter, since that is the base MIME type for all files.

Who sets the MIME type?

During the ObjectType step in the request handling process, the server determines the MIME type attributes of the resource requested by the client. Several different server application functions (SAFs) can be used to determine the MIME type, but the most commonly used one is type-by-extension.


1 Answers

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);
}

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.

like image 99
BalusC Avatar answered Sep 27 '22 17:09

BalusC