I am creating an application that for some reasons shall only accept PNG images.
How can I check if an image is really a PNG image? Currently I am using the JavaFX 2 Image class to load the image.
Thanks for any hint!
I am creating an application that for some reasons shall only accept PNG images.
How can I check if an image is really a PNG image? Currently I am using the JavaFX 2 Image class to load the image.
Thanks for any hint!
If you don't trust the filename, a simple check is to read the first 8 bytes (with FileInputStream
), and check that they correspond to the PNG signature (related: https://stackoverflow.com/a/10555053/277304)
I do this currently in my own application by confirming the file's Mime Type before I process it. There are several SO threads suggesting how to obtain Mime Types in Java.