Check if the file exists on the remote server using its URL

How can I check Java if the file exists on a remote server (served by HTTP) with its own URL? I do not want to download the file, just check its existence.

+52
java url networking file-exists
Jan 04 '11 at 17:18
source share
6 answers
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 the URL ( with HttpURLConnection ) is returned with HTTP status code 200 , then the file exists.

EDIT: Please note that since we only care about this, there is no need to request the entire document. We can simply request a header using the HTTP HEAD request method to check if it exists.

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

+79
Jan 04 '11 at 17:22
source share
 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; } } 

Check for presence or absence of URL

+14
Jan 04 '11 at 17:20
source share

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

+4
Jan 04 '11 at 17:20
source share

Check it out, it works for me. Source URL: Check if the URL exists on the 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(); } } } } 
+4
Nov 03 '15 at 9:56
source share

The only sure way is to download it :). On some servers, you can usually quit by issuing the HEAD request requested by the GET request for the same URL. This will return you only the resource metadata, not the actual contents of the file.

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

+3
Jan 04 2018-11-11T00:
source share

Make a URLConnection for it. If you succeed, it exists. You may have to go as far as opening the input stream, but you do not need to read the contents. You can immediately close the stream.

0
Jan 04 '11 at 17:20
source share



All Articles