My requirement is to check whether the file is jpeg / jpg / png . I have written the following code.
public boolean isFileValid(File file) throws IOException {
DataInputStream input = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
int fileSignature = input.readInt();
input.close();
logger.info(fileSignature);
if (fileSignature == 0xffd8ffe0) {
logger.info("File is jpeg");
return true;
} else if(fileSignature == 0x89504E47){
logger.info("file is in PNG");
return true;
} else {return false;}
}
I am using ubuntu 13.04 and the code above works fine for me.I read that the file signature doesnot vary across various O.S.(i am not very sure of this though as i havenot tested it in other O.S.).Also is it possible to vary the signature of the files? Is there a better way of doing this without using third party libraries?