There are 2 approaches for getting a file mime type:
- Identifying by the file's magic number - this is a reliable approach but it requires reading information from the file
- Guessing it by the file extension - this is a fast approach, but can be less accurate
The following solution use the 1st approach:
- Apache Tika - a toolkit for detecting and extracting metadata and structured text content from various documents using existing parser libraries
- JMimeMagic - a Java library for determining the MIME type of files or streams
- mime-util - enable Java programs to detect MIME types based on file extensions, magic data and content sniffing
The following solution use the 2nd approach:
- javax.activation.MimetypesFileTypeMap - this part of the JavaBeans Activation Framework. The MimetypesFileTypeMap looks in various places in the user's system for MIME types file entries.
- Using java.net.URL - mapping between the extension and the mime-type is defined in the file [jre_home]/lib/content-types.properties
For some more information see this post