JavaFX: get the format (PNG, JPG ..) of an image?

I am creating an application that only accepts PNG images for some reason.

How can I check if an image is really a PNG image? I am currently using the JavaFX 2 Image class to load an image.

Thanks for any hint!

+3


source to share


2 answers


I do this in my own application, validating the Mime Type file before processing it. There are several SO threads out there suggesting how to get Mime types in Java.



+2


source


If you don't trust the filename, a simple check is to read the first 8 bytes (s FileInputStream

) and check if they match the PNG signature (related: fooobar.com/questions/1224133 / ... )



+2


source







All Articles