Gigalala
Gigalala

Reputation: 489

how to check if a file is an png image file,using java

I am building an automate png to jpg, everything is built, but I have a problem detecting png files. Now I am using the file name: I am checking if the end of the file matches .png but this is not working for png files that do not end with .png.

Any ideas?

Upvotes: 7

Views: 9372

Answers (4)

Erik Lievaart
Erik Lievaart

Reputation: 433

I downloaded some images from a website using a crawler. All of the images were listed in the source as JPG, but there were some PNG mixed in. The browser is flexible, because the web is messy, but not all desktop applications process PNG files with a .jpg extension correctly. I tried the various solutions in the accepted answer, but all of them use the file name for identification of the MIME type. I checked the source (java8) of MimetypesFileTypeMap and even if you pass it a File, it just gets the file name and uses that the determine the mime type.

So instead I opted to read the 4 byte file header to determine whether it was a JPG or PNG as suggested by Itay Karo. For JPG the first byte would be FF, for PNG it would be 89 (HEX).

private static boolean isPng(File file) throws IOException {
    try (FileInputStream is = new FileInputStream(file)) {
        return Integer.toHexString(is.read).equals("89");
    }
}

which is equal to 139 (base 10), so a slightly more efficient solution is:

private static boolean isPng(File file) throws IOException {
    try (FileInputStream is = new FileInputStream(file)) {
        return is.read() == 137;
    }
}

Note that this enough to differentiate between JPG and PNG, but you might want to check the rest of the bytes as well for more accuracy, if you really don't know what kind of files you are dealing with. Props to Itay Karo for providing a description of the PNG header:

http://en.wikipedia.org/wiki/Portable_Network_Graphics#File_header

Oh, and one more thing I should mention. In Linux you can use "file" to query the mime type:

file -i [file]

Not an answer to the question as it is phrased, but it might be easier to write a simple shell script that runs after your java application:

for file in $(find "$root" -name '*.jpg')
do
        if file -i "$file" | grep -q 'image/png'; then
                rename="${file%.*}.png"
                echo "moving $file -> $rename"
                mv $file $rename
        fi
done

Upvotes: 2

Rijo Joseph
Rijo Joseph

Reputation: 1405

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());
}

Upvotes: 3

Itay Karo
Itay Karo

Reputation: 18286

You can check the header of the file.
See http://en.wikipedia.org/wiki/Portable_Network_Graphics#File_header.

Upvotes: 2

JWqvist
JWqvist

Reputation: 717

You can use Files.probeContentType(path) to get the mime type

Upvotes: 2

Related Questions