Uday Konduru
Uday Konduru

Reputation: 213

How to validate image header in java

I've a web page that that can be used to upload files. Now I need to check if the image file type is of correct type like png,jpg,jpeg,gif

I am using the mimeType that comes with the request ,but if i am loading the .txt file that was renamed to .jpg file then also mime-type it is showing image/jpg,basically i dont want to upload this files.Now I want to be sure that nobody is able to upload a .txt file that was renamed in .jpg/.png....

For reference i am putting piece of code here:

  //storing images into bytearray.

byte[] bFile = baos.toByteArray();

if((bFile [i] & 0xFF) == 0xFF && (bFile[i+1] & 0xFF) == 0xD8 && (bFile[bFile.length -    2] & 0xFF) == 0xFF  && (bFile[bFile.length - 1] & 0xFF) == 0xD9) 
                    {
                       System.out.println("is Image");
                    }

The above line will only check for jpeg type but i want to check for other image header for file extensions Can someone please point out what exactly needs to be done to check for other image types?

Thanks

Upvotes: 3

Views: 5548

Answers (1)

Athanor
Athanor

Reputation: 955

I have done something like that:

/**
 * Check if the image is a PNG. The first eight bytes of a PNG file always
 * contain the following (decimal) values: 137 80 78 71 13 10 26 10 / Hex:
 * 89 50 4e 47 0d 0a 1a 0a
 */
public boolean isValidPNG(InputStream is) {
    try {
        byte[] b = new byte[8];
        is.read(b, 0, 8);
        if (Arrays.equals(b, new BigInteger("89504e470d0a1a0a",16).toByteArray())) {
            return true;
        }
    } catch (Exception e) {
        //Ignore
        return false;
    }
    return false;
}

/**
 * Check if the image is a JPEG. JPEG image files begin with FF D8 and end
 * with FF D9
 */
public boolean isValidJPEG(InputStream is, int size) {
    try {
        byte[] b = new byte[2];
        is.read(b, 0, 2);
        // check first 2 bytes:
        if ((b[0]&0xff) != 0xff || (b[1]&0xff) != 0xd8) {
            return false;
        }
        // check last 2 bytes:
        is.skip(size-4);
        is.read(b, 0, 2);
        if ((b[0]&0xff) != 0xff || (b[1]&0xff) != 0xd9) {
            return false;
        }
    } catch (Exception e) {
        // Ignore
        return false;
    }
    return true;
}

/** Check if the image is a valid GIF. GIF files start with GIF and 87a or 89a.
 * http://www.onicos.com/staff/iz/formats/gif.html
*/
public boolean isValidGIF(InputStream is) {
    try {
        byte[] b=new byte[6];
        is.read(b, 0, 6);
        //check 1st 3 bytes
        if(b[0]!='G' || b[1]!='I' || b[2]!='F') {
            return false;
        }
        if(b[3]!='8' || !(b[4]=='7' || b[4]=='9') || b[5]!='a') {
            return false;
        }
    } catch(Exception e) {
        // Ignore
        return false;
    }
    return true;
}

There is also a PHP topic with some image headers: PHP : binary image data, checking the image type

Upvotes: 9

Related Questions