Getting A File's Mime Type In Java

后端 未结 23 2760
南方客
南方客 2020-11-21 06:53

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.

Th

相关标签:
23条回答
  • 2020-11-21 07:21

    Unfortunately,

    mimeType = file.toURL().openConnection().getContentType();
    

    does not work, since this use of URL leaves a file locked, so that, for example, it is undeletable.

    However, you have this:

    mimeType= URLConnection.guessContentTypeFromName(file.getName());
    

    and also the following, which has the advantage of going beyond mere use of file extension, and takes a peek at content

    InputStream is = new BufferedInputStream(new FileInputStream(file));
    mimeType = URLConnection.guessContentTypeFromStream(is);
     //...close stream
    

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

    0 讨论(0)
  • 2020-11-21 07:22

    I tried several ways to do it, including the first ones said by @Joshua Fox. But some don't recognize frequent mimetypes like for PDF files, and other could not be trustable with fake files (I tried with a RAR file with extension changed to TIF). The solution I found, as also is said by @Joshua Fox in a superficial way, is to use MimeUtil2, like this:

    MimeUtil2 mimeUtil = new MimeUtil2();
    mimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.MagicMimeMimeDetector");
    String mimeType = MimeUtil2.getMostSpecificMimeType(mimeUtil.getMimeTypes(file)).toString();
    
    0 讨论(0)
  • 2020-11-21 07:22

    This is the simplest way I found for doing this:

    byte[] byteArray = ...
    InputStream is = new BufferedInputStream(new ByteArrayInputStream(byteArray));
    String mimeType = URLConnection.guessContentTypeFromStream(is);
    
    0 讨论(0)
  • 2020-11-21 07:22

    if you work on linux OS ,there is a command line file --mimetype:

    String mimetype(file){
    
       //1. run cmd
       Object cmd=Runtime.getRuntime().exec("file --mime-type "+file);
    
       //2 get output of cmd , then 
        //3. parse mimetype
        if(output){return output.split(":")[1].trim(); }
        return "";
    }
    

    Then

    mimetype("/home/nyapp.war") //  'application/zip'
    
    mimetype("/var/www/ggg/au.mp3") //  'audio/mp3'
    
    0 讨论(0)
  • 2020-11-21 07:22
    File file = new File(PropertiesReader.FILE_PATH);
    MimetypesFileTypeMap fileTypeMap = new MimetypesFileTypeMap();
    String mimeType = fileTypeMap.getContentType(file);
    URLConnection uconnection = file.toURL().openConnection();
    mimeType = uconnection.getContentType();
    
    0 讨论(0)
  • 2020-11-21 07:22

    Apache Tika.

    <!-- https://mvnrepository.com/artifact/org.apache.tika/tika-parsers -->
    <dependency>
        <groupId>org.apache.tika</groupId>
        <artifactId>tika-parsers</artifactId>
        <version>1.24</version>
    </dependency>
    
    

    and Two line of code.

    Tika tika=new Tika();
    tika.detect(inputStream);
    

    Screenshot below

    0 讨论(0)
提交回复
热议问题