Ambika Balakrishnan
Ambika Balakrishnan

Reputation: 65

How to find the mime type of video files in java

I have an application, in that am uploading mp4 and flv video files. In case, if the user changed the extension of some other file as 'mp4'/'flv' means the file is uploaded and the video is not playing. How can I check the mime type of mp4/flv video files in java.

I am struggling with this please anyone can help me to complete this task.

Upvotes: 1

Views: 6501

Answers (1)

Shishir Kumar
Shishir Kumar

Reputation: 8201

If you have Java 7, then you can make use of Files.probeContentType(path).

import java.nio.file.*;
 public class MimeTypes {
       public static void main(String[] args) {
          Path path;
          try {
             path = Paths.get("/etc");  
             System.out.println( path + " : " + Files.probeContentType(path) );

             path = Paths.get("/dev/null");  
             System.out.println( path + " : " + Files.probeContentType(path) );

             path = Paths.get("/var/log/syslog.2.gz");  
             System.out.println( path + " : " + Files.probeContentType(path) );

             path = Paths.get("/var/run/rpcbind.sock");  
             System.out.println( path + " : " + Files.probeContentType(path) );

             path = Paths.get("abc.mp4");  
             System.out.println( path + " : " + Files.probeContentType(path) );

             path = Paths.get("MimeTypes.java");  
             System.out.println( path + " : " + Files.probeContentType(path) );
          } catch (Exception x) {
          }
       }
    }

Output:

# java MimeTypes 
/etc : inode/directory
/dev/null : inode/chardevice
/var/log/syslog.2.gz : application/x-gzip
/var/run/rpcbind.sock : inode/socket
abc.mp4 : video/mp4
MimeTypes.java : text/x-java

Oracle documentation of Files is here.

Another option: Apache Tika

Apache Tika is a toolkit for detecting and extracting metadata and structured text content from various documents using existing parser libraries. Tika has a lot of dependencies ... almost 20 jars ! But it can do a lot more than detecting filetype. For example, you can parse a PDF or DOC to extract the text and the metadata very easily.

public static void main(String args[]) throws Exception {

    FileInputStream is = null;
    try {
      File f = new File("C:/Temp/mime/abc.mp4");
      is = new FileInputStream(f);

      ContentHandler contenthandler = new BodyContentHandler();
      Metadata metadata = new Metadata();
      metadata.set(Metadata.RESOURCE_NAME_KEY, f.getName());
      Parser parser = new AutoDetectParser();
      // OOXMLParser parser = new OOXMLParser();
      parser.parse(is, contenthandler, metadata);
      System.out.println("Mime: " + metadata.get(Metadata.CONTENT_TYPE));
      System.out.println("Title: " + metadata.get(Metadata.TITLE));
      System.out.println("Author: " + metadata.get(Metadata.AUTHOR));
      System.out.println("content: " + contenthandler.toString());
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    finally {
        if (is != null) is.close();
    }
  }

Upvotes: 4

Related Questions