Java – How to get a file’s Media Type (MIME type)

javamime

How do you get a Media Type (MIME type) from a file using Java? So far I've tried JMimeMagic & Mime-Util. The first gave me memory exceptions, the second doesn't close its streams properly.

How would you probe the file to determine its actual type (not merely based on the extension)?

Best Answer

In Java 7 you can now just use Files.probeContentType(path).