I was just wondering how most people fetch a mime type from a file in Java? So far I've tried two utils: JMimeMagic
& Mime-Util
.
The first gave me memory exceptions, the second doesn't close its streams off properly. I was just wondering if anyone else had a method/library that they used and worked correctly?
You can do it with just one line: MimetypesFileTypeMap().getContentType(new File("filename.ext")). Look the complete test code (Java 7):
This code produces the follow output: text/plain
To chip in with my 5 cents:
TL,DR
I use MimetypesFileTypeMap and add any mime that is not there and I specifically need it, into mime.types file.
And now, the long read:
First of all, MIME types list is huge, see here: https://www.iana.org/assignments/media-types/media-types.xhtml
I like to use standard facilities provided by JDK first, and if that doesn't work, I'll go and look for something else.
Determine file type from file extension
Since 1.6, Java has MimetypesFileTypeMap, as pointed in one of the answers above, and it is the simplest way to determine mime type:
In its vanilla implementation this does not do much (i.e. it works for .html but it doesn't for .png). It is, however, super simple to add any content type you may need:
Example entries for png and js files would be:
For mime.types file format, see more details here: https://docs.oracle.com/javase/7/docs/api/javax/activation/MimetypesFileTypeMap.html
Determine file type from file content
Since 1.7, Java has java.nio.file.spi.FileTypeDetector, which defines a standard API for determining a file type in implementation specific way.
To fetch mime type for a file, you would simply use Files and do this in your code:
The API definition provides for facilities that support either for determining file mime type from file name or from file content (magic bytes). That is why probeContentType() method throws IOException, in case an implementation of this API uses Path provided to it to actually try to open the file associated with it.
Again, vanilla implementation of this (the one that comes with JDK) leaves a lot to be desired.
In some ideal world in a galaxy far, far away, all these libraries which try to solve this file-to-mime-type problem would simply implement java.nio.file.spi.FileTypeDetector, you would drop in the preferred implementing library's jar file into your classpath and that would be it.
In the real world, the one where you need TL,DR section, you should find the library with most stars next to it's name and use it. For this particular case, I don't need one (yet ;) ).
Unfortunately,
does not work, since this use of URL leaves a file locked, so that, for example, it is undeletable.
However, you have this:
and also the following, which has the advantage of going beyond mere use of file extension, and takes a peek at content
However, as suggested by the comment above, the built-in table of mime-types is quite limited, not including, for example, MSWord and PDF. So, if you want to generalize, you'll need to go beyond the built-in libraries, using, e.g., Mime-Util (which is a great library, using both file extension and content).
I've published my SimpleMagic Java package which allows content-type (mime-type) determination from files and byte arrays. It is designed to read and run the Unix file(1) command magic files that are a part of most ~Unix OS configurations.
I tried Apache Tika but it is huge with tons of dependencies,
URLConnection
doesn't use the bytes of the files, andMimetypesFileTypeMap
also just looks at files names.With SimpleMagic you can do something like:
in spring MultipartFile file;
file.getContentType();