How to check if file exists on remote server using its URL

If the connection to a URL (made with HttpURLConnection) returns with HTTP status code 200 then the file exists.

public static boolean exists(String URLName){
    try {
      // note : you may also need
      //        HttpURLConnection.setInstanceFollowRedirects(false)
      HttpURLConnection con =
         (HttpURLConnection) new URL(URLName).openConnection();
      return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
    catch (Exception e) {
       return false;

Note that since we only care it exists or not there is no need to request the entire document. We can just request the header using the HTTP HEAD request method to check if it exists.

Uday Ogra

Connect with me at and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *