[Java] Various methods of obtaining MIME type based on file

Source: Internet
Author: User

1,Mime-util:

The tool can detect MIME types by detecting file extensions, or by detecting magic header two technical ways.

Reliable and lightweight, relying only on slf4j a package . It hasn't been updated in 2010 years.

POM

<dependency>    <groupId>eu.medsea.mimeutil</groupId>    <artifactid>mime-util</ Artifactid>    <version>2.1.3</version></dependency>


Example code:

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);  Output:application/msword      }  }  

2.Jmimemagic It is obviously not a robust method to find the file type by detecting the file suffix name. Jmimemagic is a Java toolkit that examines the MIME type of a file or stream by examining the magic headers. it is still under continuous updating and recommended. POM
<dependency>    <groupId>net.sf.jmimemagic</groupId>    <artifactid>jmimemagic</ Artifactid>    <version>0.1.4</version></dependency>
Sample code
Magic parser = new Magic ();  Getmagicmatch accepts Files or byte[],  //which is nice if you want to test streams  magicmatch match = PARSER.G Etmagicmatch (New File ("Gumby.gif"));  System.out.println (Match.getmimetype ());  

3.Java.net.URL similar to the above-mentioned match suffix name. The mapping of suffix names and mime-type is defined in the [Jre_home]\lib\content-types.propertiesThis component only determines the Mime-type based on the file name extension . Not very reliable.
Sample code (recommended)
Import Java.net.FileNameMap;  Import java.net.URLConnection;    public class FileUtils {public      static string GetMimeType (String fileUrl)        throws Java.io.IOException      {        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 "));        Output:  text/plain      }    
Sample code (not recommended: This is also determined by the extension, MIME type, and very slow)
Import java.net.*;    public class fileutils{Public    static string GetMimeType (String fileUrl)      throws Java.io.IOException, 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 "));      Output:  text/plain    }  }  

4.Javax.activation.MimetypesFileTypeMap need to introduce Activation.jar this jar package, he can get from this websiteHttp://java.sun.com/products/javabeans/glasgow/jaf.html. 
the Mimetypesfilemap class maps a file's MIME type, which is defined in the resource file in the Activation.jar package .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 propertiesSample Code
Import Javax.activation.MimetypesFileTypeMap;  Import Java.io.File;    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"    }  }  



[Java] Various methods of obtaining MIME type based on file

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: info-contact@alibabacloud.com 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.