Get file’s mime-type

To get file’s mime-type I usually use tika-app-1.3.jar library.

You can download it here .

In this way you can use the tika library to obtein the mime-type.

 

public static String getMimeFromFialeTika(String nomeFile ) throws Exception {

       InputStream fileStream = null ;

       org.apache.tika.mime.MediaType mediaType = null ;

       try {

             File file = new File(nomeFile);

             fileStream = new FileInputStream(file);

             AutoDetectParser parser = new AutoDetectParser();

             Detector detector = parser.getDetector();

             org.apache.tika.metadata.Metadata md = new org.apache.tika.metadata.Metadata();

             md.add(org.apache.tika.metadata.Metadata.RESOURCE_NAME_KEY, file.getName() );

             BufferedInputStream bis = new BufferedInputStream(fileStream);

             mediaType = detector.detect(bis, md);

       } finally {

             try {

                    if (fileStream!=null) fileStream.close();

             } catch(Exception error) {

                    throw error;

             }

       }

       return mediaType.toString();

}