Wednesday, August 10, 2011

Servlet Mime Mappings and Windows

 

The following issue was encountered in Tomcat 6, Servlet API 2.5.  Your experiences in other app servers/versions may vary.

In Tomcat, you can set Mime Mappings in your web.xml like so:

	
<mime-mapping>
<extension>gif</extension>
<mime-type>image/gif</mime-type>
</mime-mapping>

The servlet container will then automatically detect a mime type for uploaded files using some built-in entries, as well as the ones you define in web.xml.


Oh That’s Good…


Unfortunately, this extension mapping is case sensitive.  So suddenly your digital camera, which saves files with upper case extensions (I’m looking at you Nikon), such as DCN005.JPG will foil your app server.


Oh That’s Bad….


So to at least partially handle this, you should enter upper case extensions as a separate mapping.


In Tomcat, you can set Mime Mappings in your web.xml like so:

	
<mime-mapping>
<extension>gif</extension>
<mime-type>image/gif</mime-type>
</mime-mapping>
<mime-mapping>
<extension>GIF</extension>
<mime-type>image/gif</mime-type>
</mime-mapping>

Now your app will handle upper case extensions (like JPG), but it will still be foiled if the file extension is mixed case like: ex2399.JpG.


I would definitely like mime mappings to be case insensitive! Does anybody know how to do this? I used javax.activation in the past for Mimetype detection and while it was definitely harder to use than the through the servlet api, but I haven't had a chance to see if it is case-sensitive.

No comments:

Post a Comment