Java Mime Type Content Type

I’ve been working on a Java servlet these days which for many reasons needs to determine the mime type of files so that files being rendered are correctly used by my clients. For example, I’m generating a crossdomian.xml, which if not matched with the text/xml content type, really confuses flash players.

Anyway, I fumbled around for a bit in Google and found one great solution: mime-util. It’s Apache License and works great. The documentation is even very good.

Since I can trust my file names I opted for the simple file name extension detection method. It’s a perfect fit except for one thing: I need to detect the application/json content type which is not part of the default property bundle.

It took me a few minutes of digging trough the JavaDocs to find the answer, so to save you some time, here’s my solution:

  1. Create a file named mime-types.properties and put it somewhere in your class path.
  2. Add the types you need as extension=type pairs like json=application/json