mbmast mbmast - 1 year ago 126
Java Question

How to Use Metadata-extractor to Determine File Type

I'm using metadata-extractor to extract the meta data from image files and this is working great. What I'd like to know is if there is a way that the metadata-extractor library can tell me the type of file I'm processing. I have some files that are supported file types, but they lack a file extension. Is there an API that'll just give me the file type, assuming I'm passing in a supported file type?

Answer Source

Yes you can use the FileTypeDetector class.

I added a page about its use to the project wiki:


In a nutshell:

FileTypeDetector detector = new FileTypeDetector();

FileType fileType = detector.detectFileType(myStream);

if (fileType == FileType.Jpeg) {
    // ...
} else if (fileType == FileType.Png) {
    // ...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download