If you want to support only a few types of images, you can start by loading (up) the image, and at some point use the first few bytes to check if you want to continue downloading.
Quite a few image formats can be recognized using the first few bytes, a magic number . If the number matches, you don't know if the file is really valid, but it can be used to match the extension and magic number to prevent it from really matching at all.
Check out this page to check out some Java that test mime types. Read the docs or source to see if any given method requires the entire file, or can handle multiple bytes. I have not used these libraries :)
Also check out this page , which also lists some java libraries and some documents on which the discovery is based.
Be sure to add some feedback if you manage to find something you like!
source share