[Java] A variety of ways to get MIME type from a file __java

Source: Internet
Author: User

1, Mime-util:

The tool can detect the MIME type by detecting the file name extension or detecting the magic header two technical methods.

More 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>


Sample 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 by detecting file suffix name to find the file type is obviously not a robust method. Jmimemagic is a Java toolkit that checks magic headers to determine the MIME type of a file or stream. Currently in the ongoing update, recommended use. 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 with the above mentioned matching suffix name similar. The mapping relationship between the suffix name and the mime-type is defined in [jre_home]\lib\content-types.properties the component is judged only by the file name extension Mime-type. 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 the way to determine MIME type by extension and is slow)
Import java.net.*;  
  
public class fileutils{  
  the 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 obtain from the website below http://java.sun.com/products/ Javabeans/glasgow/jaf.html.
This Mimetypesfilemap class maps the MIME type of a file, which is limited in the number of Mime-type lists defined in the resource file in the Activation.jar package, 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 looks in the following order to find the MIME type

First, you add files to an instance of Mimetypesfiletypemap by using a program
Finds a 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 (usually only in Activation.jar).

This approach is very interesting when you need to process a file named after an incoming generic file. The results come out fast because only the extension is used to guess the file's natural attributes sample 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"  
  }  
}  



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: 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.