8

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?

Boris Pavlović
  • 60,971
  • 26
  • 119
  • 144
bbd
  • 81
  • 1
  • 3

1 Answers1

16

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.

unwind
  • 378,987
  • 63
  • 458
  • 590
  • 3
    and here is the code to make HEAD http://stackoverflow.com/questions/4177864/checking-a-url-exist-or-not/4177885#4177885 – jmj Jan 25 '11 at 13:24