Several ways to get file MIME types in Java (GO)

Source: Internet
Author: User

This article was reproduced from:

Using Javax.activation.MimetypesFileTypeMap
Need to introduce Activation.jar this jar package, he can get from this website.
The Mimetypesfilemap class maps a file's MIME type, which is defined in the resource file in the Activation.jar package.

Sample code

Import Javax.activation.MimetypesFileTypeMap;

Class GetMimeType {
public static void Main (String args[]) {
File F = new file ("Gumby.gif");
System.out.println ("Mime Type of" + f.getname () + "is" +
New Mimetypesfiletypemap (). getContentType (f));
Expected output:
"Mime Type of Gumby.gif is Image/gif"

The number of Mime-type in your own list is limited, but it provides a way for you to easily add more MIME types

Mimetypesfiletypemap will find the MIME type of the file in many parts of the user's system. When a request to find a MIME type arrives, he will find the MIME type in the following order

You first add the file to an instance of Mimetypesfiletypemap by using a program
Locate the file under the user's home path. mime.types
Find Files <java.home>/lib/mime.types
Find files or resources meta-inf/mime.types
Find files or resource Meta-inf/mimetypes.default (typically found only in Activation.jar).

This method is very interesting when you need to process a file named for an incoming generic file. The results come out fast because only the extension is used to guess the nature of the file's properties

Warning: This method is very slow
Similar to the above-mentioned match suffix name. The mapping of suffix names and mime-type is defined in [jre_home]\lib\ this file


public class fileutils{
public static string GetMimeType (String fileUrl)
Throws, Malformedurlexception
String type = null;
URL u = new URL (fileUrl);
URLConnection UC = null;
UC = U.openconnection ();
Type = Uc.getcontenttype ();
return type;

public static void Main (String args[]) throws Exception {
System.out.println (Fileutils.getmimetype ("File://c:/temp/test.TXT"));

From R. Lovelock's Notes:
I'm trying to find the best way to get the type of MIME type and find that your discovery is useful, but now I find that it can be found by urlconnection and not as slow as you describe.


public class FileUtils {

public static string GetMimeType (String fileUrl)
fileNameMap fileNameMap = Urlconnection.getfilenamemap ();
String type = filenamemap.getcontenttypefor (FILEURL);
return type;

public static void Main (String args[]) throws Exception {
System.out.println (Fileutils.getmimetype ("File://c:/temp/test.TXT"));

Using Apache Tika
Tika is a sub-project of Lucene, which is a toolkit for finding and extracting metadata and structured text content in various documents through an existing parsing library.
This package provides support for crime file types, including office2007 (Docs/pptx/xlsx/etc ... )

Apache Tika
Tika has a lot of dependent packages, about 20 jar packs! But it can do more than just detect file types, for example, you can parse a PDF or doc file and easily get text and metadata


Import Org.apache.tika.metadata.Metadata;
Import Org.apache.tika.parser.AutoDetectParser;
Import Org.apache.tika.parser.Parser;
Import Org.apache.tika.sax.BodyContentHandler;
Import Org.xml.sax.ContentHandler;

public class Main {

public static void Main (String args[]) throws Exception {

FileInputStream is = null;
try {
File F = new file ("C:/temp/mime/test.docx");
is = new FileInputStream (f);

ContentHandler ContentHandler = new Bodycontenthandler ();
Metadata Metadata = new Metadata ();
Metadata.set (Metadata.resource_name_key, F.getname ());
Parser Parser = new Autodetectparser ();
Ooxmlparser parser = new Ooxmlparser ();
Parser.parse (IS, contenthandler, metadata);
System.out.println ("Mime:" + metadata.get (metadata.content_type));
System.out.println ("Title:" + metadata.get (metadata.title));
System.out.println ("Author:" + metadata.get (;
System.out.println ("content:" + contenthandler.tostring ());
catch (Exception e) {
E.printstacktrace ();
finally {
if (is! = null) is.close ();


Using Jmimemagic
It is obviously not a robust method to find the file type by detecting the file suffix name. The Jmimemagic library provides a more robust detection method, a Java toolkit that examines the MIME type of a file or stream by examining the magic headers

Snippet for Jmimemagic Lib

Magic parser = new Magic ();
Getmagicmatch accepts Files or byte[],
Which is nice if you want to test streams
MagicMatch match = Parser.getmagicmatch (new File ("Gumby.gif"));
System.out.println (Match.getmimetype ());

Using Mime-util

Another tool is mime-util, a tool that can detect MIME types by detecting file extensions or detecting magic headers in two technical ways

Import Eu.medsea.mimeutil.MimeUtil;
public class Main {
public static void Main (string[] args) {
Mimeutil.registermimedetector ("Eu.medsea.mimeutil.detector.MagicMimeMimeDetector");
File F = new file ("C:/temp/mime/test.doc");
collection<?> mimetypes = Mimeutil.getmimetypes (f);
System.out.println (mimetypes);

The good thing about Mime-util is that it is lightweight and relies only on slf4j a package

Several ways to get file MIME types in Java (GO)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.