Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if URL is an image?

Tags:

java

url

I would like to check if URL given by user is image (jpg, png, gif). First idea: check only file extension in URL. Second idea: get this resource from server (by http get) and load into some Java picture library to get info if is it image (seriously disadvantage: slow). Or maybe yet another solution?

like image 543
bbd Avatar asked Jan 25 '11 13:01

bbd


People also ask

Can a URL be an image?

A URL is a web address that specifies location. Therefore, an image URL is a web address that specifies the location of an image.

Does every image have a URL?

Every image needs a URL because every image is a unique image. mysite.com/img/potato.png and mysite.com/img/apple.png are 2 separate images on my website for example.


1 Answers

You should use HTTP HEAD, not a full GET. This should include the Content-Type as known by the server. You could of course test the extension first, and only do the expensive/slow HTTP roundtrip if it's inconclusive.

like image 190
unwind Avatar answered Sep 28 '22 09:09

unwind