How to check if a file is a PNG file using java

I am going to automate png to jpg, all this is bullets, but I have a problem with detecting png files, now I use the file name and I check the ending if it matches ".png", but this does not work with files that are files png and do not end with ".png". any ideas?

+4
source share
3 answers

u can try this

import javax.activation.MimetypesFileTypeMap;
import java.io.File;

class GetMimeType {
  public static void main(String args[]) {
    File f = new File(filePath);
    System.out.println("Mime Type of " + f.getName() + " is " +
                         new MimetypesFileTypeMap().getContentType(f));

}

or

try

public String getContentType(File file) throws IOException {
        return Files.probeContentType(file.getAbsolutePath());
}
+1
source

Files.probeContentType(path) mime

+1

All Articles