Reputation:
I want to know if a specific file exists a on server or not. For example, suppose I have an .xml
file on my server and I want to know if the file is there or not through java from my android application.
Upvotes: 1
Views: 836
Reputation: 1062
You could use something like this:
URL url = null;
URLConnection connection = null;
InputStreamReader stream = null;
// Parse the URL
try {
url = new URL(urlString);
} catch (MalformedURLException e1) {
System.out.println("Malformed URL");
return null;
}
// Open a connection to the URL
try {
connection = url.openConnection();
} catch (IOException e) {
System.out.println("Could not create connection");
return null;
}
// Get the remote file
try {
stream = new InputStreamReader(connection.getInputStream());
} catch (IOException e) {
System.out.printf("File \"%s\" does not exist!",urlString);
return null;
}
Upvotes: 0
Reputation: 1934
What server is it? If its HTTP server, you can request that file and check from response if it exist. If its custom server you have to implement that feature yourself.
Upvotes: 2