Apache Tika - Detect MIME Type of any file

Apache Tika - a content analysis toolkit

The Apache Tika™ toolkit detects and extracts metadata and structured text content from various documents using existing parser libraries. You can find the latest release on the download page.


JAVA class to detect MIME Type of the file MIMETypeDetector.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.tika.Tika;
import org.apache.tika.io.TikaInputStream;
/** 
 * @author tejas.purohit
 * 
 **/
public class MIMETypeDetector {

 /*** Logger instance for logging info*/
 private final static Logger log = Logger.getLogger(MIMETypeDetector.class.getName());

 /*** Detect and return MIME Type of the given file. 
  * @param file java.io.File to detect MIME Type
  * @return MIME Type of the file as String
  */
 public static String getMimeType(File file) {
  String mimeType = null;
  try {
   Tika tika = null;
   TikaInputStream is = null;

   // Creating new Instance of org.apache.tika.Tika
   tika = new Tika();

   // Detecting MIME Type of the File
   mimeType = tika.detect(file);
  } catch (FileNotFoundException e) {
   log.log(Level.WARNING, "Specified File not found", e);
  } catch (IOException e) {
   log.log(Level.WARNING, "IO Error", e);
  }
  // returning detected MIME Type
  return mimeType;
 }

 /** 
  * Detect and return MIME Type of the given file. 
  * @param filePath full path of the file to detect MIME Type
  * @return MIME Type of the file as String
  */
 public static String getMimeType(String filePath) {

  // Creating new instance of java.io.File for given file path
  File f = new File(filePath);

  // Getting and returning MIME Type of the file
  return getMimeType(f);
 }

 public static void main(String[] args) {

  // Getting file path as command line argument
  String filePath = args[0];

  // Getting and printing MIME Type of the given file
  System.out.println("Is ZIP file:" + MIMETypeDetector.getMimeType(filePath));
 }
}

Comments