javametadata-extractor

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?


Solution

  • Yes you can use the FileTypeDetector class.

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

    https://github.com/drewnoakes/metadata-extractor/wiki/File-Type-Detection

    In a nutshell:

    FileType fileType = FileTypeDetector.detectFileType(myStream);
    
    if (fileType == FileType.Jpeg) {
        // ...
    } else if (fileType == FileType.Png) {
        // ...