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