Check if file exists on remote server using its URL

JavaHttpNetworkingUrlFile Exists

Java Problem Overview


How can I check in Java if a file exists on a remote server (served by HTTP), having its URL? I don't want to download the file, just check its existence.

Java Solutions


Solution 1 - Java

import java.net.*;
import java.io.*;

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

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

EDIT: 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.

Source: http://www.rgagnon.com/javadetails/java-0059.html

Solution 2 - Java

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

https://stackoverflow.com/questions/4177864/checking-a-url-exist-or-not

Solution 3 - Java

Check this, it works for me. Source URL: https://stackoverflow.com/questions/26418486/check-if-url-exists-or-not-on-server

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);     

    String customURL = "http://www.desicomments.com/dc3/08/273858/273858.jpg";

    MyTask task = new MyTask();
    task.execute(customURL);
}


private class MyTask extends AsyncTask<String, Void, Boolean> {

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected Boolean doInBackground(String... params) {

         try {
                HttpURLConnection.setFollowRedirects(false);
                HttpURLConnection con =  (HttpURLConnection) new URL(params[0]).openConnection();
                con.setRequestMethod("HEAD");
                System.out.println(con.getResponseCode()); 
                return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
            }
            catch (Exception e) {   
                e.printStackTrace();    
                return false;
            }
    }

    @Override
    protected void onPostExecute(Boolean result) {
        boolean bResponse = result;
         if (bResponse==true)
            {
                Toast.makeText(MainActivity.this, "File exists!", Toast.LENGTH_SHORT).show();      
            }
            else
            {           
                Toast.makeText(MainActivity.this, "File does not exist!", Toast.LENGTH_SHORT).show();
            }                  
    }           
}
}

Solution 4 - Java

Assuming the file is being served through http, you can send a HEAD request to the URL and check the http response code returned.

Solution 5 - Java

The only true way is to download it :). On some servers usually you can get away by issuing a HEAD request insted of a GET request for the same url. This will return you only the resource metadata and not the actual file content.

Update: Check org.life.java's answer for the actual technical details on how to do this.

Solution 6 - Java

Make a URLConnection to it. If you succeed, it exists. You may have to go so far as opening an input stream to it, but you don't have to read the contents. You can immediately close the stream.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRuiView Question on Stackoverflow
Solution 1 - JavaAdamView Answer on Stackoverflow
Solution 2 - JavajmjView Answer on Stackoverflow
Solution 3 - JavaIshtiyaq HusainView Answer on Stackoverflow
Solution 4 - JavaMike TunnicliffeView Answer on Stackoverflow
Solution 5 - JavaMihai ToaderView Answer on Stackoverflow
Solution 6 - JavarfeakView Answer on Stackoverflow