1

Is there any API in Java that can return the mime-type or content-Type as video/mp4 if I pass it .mp4 extension or complete filename?

Thanks

NewQueries
  • 4,841
  • 11
  • 33
  • 39

2 Answers2

0

You can use the java.nio.file.Files#ProbeFileContent(path) which will by default use a extension-detector unless you have havnt registered any other java.nio.file.spi.FileTypeDetector:s

public class Main {
    public static void main(String[] argv) throws Throwable {
        Path path = Paths.get("c:/test.mp4");
        String contentType = Files.probeContentType(path);

        System.out.println(contentType);
    }
}

will output:

video/mp4
Aksel Willgert
  • 11,367
  • 5
  • 53
  • 74
0

I've recently released my SimpleMagic package which implements the same functionality as the Unix file(1) command. It uses either internal config files or can read /etc/magic, /usr/share/file/magic, or other magic(5) files and determine file content from File, InputStream, or byte[]. It actually uses the content bytes of the file instead of just the name however.

Location of the github sources, javadocs, and some documentation are available from the home page.

Gray
  • 115,027
  • 24
  • 293
  • 354
  • I have the impression that the question is poorly formulated and that the OP is essentially asking exactly the same question as already answered here: http://stackoverflow.com/questions/2244432/how-should-response-content-type-for-documents-be-specified-in-order-to-work-con/2244521#2244521 In other words, his concrete problem is that he got `null` while using `ServletContext#getMimeType()` on a `.mp4` file. – BalusC Jul 24 '13 at 19:45
  • Maybe @BalusC. Thought my mimetype library might help somehow. Thanks. – Gray Jul 24 '13 at 19:53