senzacionale
senzacionale

Reputation: 20916

can I check if a file exists at a URL?

can I check if a file exists at a URL?

This link is very good for C#, what about java. I serach but i did not find good solution.

Upvotes: 10

Views: 14200

Answers (2)

user156676
user156676

Reputation:

It's quite similar in Java. You just need to evaluate the HTTP Response code:

final URL url = new URL("http://some.where/file.html");
url.openConnection().getResponseCode();

A more complete example can be found here.

Upvotes: 13

Han
Han

Reputation: 5642

Contributing a clean version that's easier to copy and paste.

try {
    final URL url = new URL("http://your/url");
    HttpURLConnection huc = (HttpURLConnection) url.openConnection();
    int responseCode = huc.getResponseCode();
    // Handle response code here...
} catch (UnknownHostException uhe) {
    // Handle exceptions as necessary
} catch (FileNotFoundException fnfe) {
    // Handle exceptions as necessary
} catch (Exception e) {
    // Handle exceptions as necessary
}

Upvotes: 5

Related Questions