Getting and Extending mime-types in Java

Working with mime-types (e.g. in your own servlet environment) is very easy, if you know the basics implemented in Java. The main object we need is the java.net.FileNameMap. Its method #getContentTypeFor(String fileName) returns the mime-type for the desired filename. You can get a default implementation of FileNameMap from java.net.URLConnection#getFileNameMap(). (Btw: There are different subclasses of URLConnection to handle jars or URLs.)

The default mime-types are generally read from the properties file JAVA_HOME/lib/content-types.properties. There you can find only the most important ones, not really useable in a complex servlet environment. But you can copy this properties file, adapt it to your special needs and force the URLConnection to load it. You just have to set the system property content.types.user.table to your own properties file.

,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: