8

This method should be check if a file is MIME type. To achieve this goal I am using method probeContentType().
However maybe is another way to decide same question. Which are the other alternatives (if there are any)?

Code:

class ProbeContentTypeCheker implements Checker {

    @Override
    public boolean check(File fileCheck) {
        try {
            Path filePath = FileSystems.getDefault().getPath(
                    fileCheck.getAbsolutePath());
            if ((Files.probeContentType(filePath) != null)) {
                return true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return false;
    }
}

Question:

  • Are there other alternatives to check MIME type in files?
gaborsch
  • 15,408
  • 6
  • 37
  • 48
catch23
  • 17,519
  • 42
  • 144
  • 217

3 Answers3

6

There are 2 approaches for getting a file mime type:

  1. Identifying by the file's magic number - this is a reliable approach but it requires reading information from the file
  2. Guessing it by the file extension - this is a fast approach, but can be less accurate

The following solution use the 1st approach:

  1. Apache Tika - a toolkit for detecting and extracting metadata and structured text content from various documents using existing parser libraries
  2. JMimeMagic - a Java library for determining the MIME type of files or streams
  3. 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:

  1. 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.
  2. 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

Dror Bereznitsky
  • 20,048
  • 3
  • 48
  • 57
  • 1
    Can you add another variants checking MIME types (enumerate list)? – catch23 Mar 10 '13 at 10:15
  • Do you mind me asking, if you can see at [this SO Question](http://stackoverflow.com/questions/15325047/check-file-of-mime-type-with-jmimemagic). – catch23 Mar 10 '13 at 22:00
  • Thanks. I ended up using `mime-util`, it works great even on a 9GB+ file. – Bob Sep 15 '13 at 20:04
1

You can use String mimeType = new MimetypesFileTypeMap().getContentType(theFile);.

Note that if no MIME type is found, application/octet-stream is returned instead of null.

sp00m
  • 47,968
  • 31
  • 142
  • 252
  • 1
    `getContentType(theFile)` => Can we check there `theFile.getAbsolutePath()`? And after fixed - is this valid [`check(File fileCheck)`](http://pastebin.com/SpmFGZGf)? – catch23 Mar 01 '13 at 11:10
  • 1
    public boolean check(File fileCheck) { String mimeType = new MimetypesFileTypeMap().getContentType(fileCheck); if (mimeType == null) { return false; } return true; } - Is this correct? – catch23 Mar 01 '13 at 11:20
  • @nazar_art No, since as I said, if no MIME type is found, `application/octet-stream` is returned instead of `null`. `application/octet-stream` is usually the default MIME type of any file, if no other more specific has been found. – sp00m Mar 01 '13 at 13:13
  • 1
    If this [MimetypesFileTypeMap()](http://pastebin.com/bcpyEgrw) isn't correct? How we can repair this method and to prove good work file check method? – catch23 Mar 01 '13 at 14:47
  • Copy the source of class MimetypesFileTypeMap and set static variable 'defaultType' null. But I would not do that unless there is a specific reason. It is possible that you may need additional mime types added, see http://stackoverflow.com/questions/6308142/adding-mimetypes-to-mimetypesfiletypemap – Adisesha Mar 05 '13 at 13:06
  • @Adi What is better variant at this situations? – catch23 Mar 09 '13 at 20:53
0

Another alternative is to Use URLConnection.guessContentTypeFromName(String fileName) if you cannot use Java 7. Note that if the content type cannot be guessed, the method will return null.

fboulay
  • 401
  • 3
  • 5