Check If File Exists on Remote Server Using Its Url

Check if file exists on remote server using its URL

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

Android checks if file exists in a remote server using its URL

I believe you are doing this in your main thread. Thats the reason its not working, you cant perform network operations in your main thread.

Try putting the code in AsyncTask or Thread.

Edit 1: As a quick fix try wrapping your "file checking code" like this:

    new Thread() {

public void run() {
//your "file checking code" goes here like this
//write your results to log cat, since you cant do Toast from threads without handlers also...

try {
HttpURLConnection.setFollowRedirects(false);
// note : you may also need
//HttpURLConnection.setInstanceFollowRedirects(false)

HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection();
con.setRequestMethod("HEAD");
if( (con.getResponseCode() == HttpURLConnection.HTTP_OK) )
log.d("FILE_EXISTS", "true");
else
log.d("FILE_EXISTS", "false");
}
catch (Exception e) {
e.printStackTrace();
log.d("FILE_EXISTS", "false");;
}
}
}.start();

check if file exists on remote host with ssh

Here is a simple approach:

#!/bin/bash
USE_IP='-o StrictHostKeyChecking=no username@192.168.1.2'

FILE_NAME=/home/user/file.txt

SSH_PASS='sshpass -p password-for-remote-machine'

if $SSH_PASS ssh $USE_IP stat $FILE_NAME \> /dev/null 2\>\&1
then
echo "File exists"
else
echo "File does not exist"

fi

You need to install sshpass on your machine to work it.

Check if file exists on remote server and various drive

In UNC paths, drives are represented by a $. That is, D$. Try this:

System.IO.File.Exists(@"\\ourvideoserver\D$\pcode\videofile_name.mp4")

How to check if a file exists on an webserver by its URL?

You can use .NET to do a HEAD request and then look at the status of the response.

Your code would look something like this (adapted from The Lowly HTTP HEAD Request):

// create the request
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

// instruct the server to return headers only
request.Method = "HEAD";

// make the connection
HttpWebResponse response = request.GetResponse() as HttpWebResponse;

// get the status code
HttpStatusCode status = response.StatusCode;

Here's a list detailing the status codes that can be returned by the StatusCode enumerator.

IOS: Check existence of remote file

**Use this function below to check whether file exists at specified url**

+(void)checkWhetherFileExistsIn:(NSURL *)fileUrl Completion:(void (^)(BOOL success, NSString *fileSize ))completion
{
//MAKING A HEAD REQUEST
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:fileUrl];
request.HTTPMethod = @"HEAD";
request.timeoutInterval = 3;

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
if (connectionError == nil) {
if ((long)[httpResponse statusCode] == 200)
{
//FILE EXISTS

NSDictionary *dic = httpResponse.allHeaderFields;
NSLog(@"Response 1 %@",[dic valueForKey:@"Content-Length"]);
completion(TRUE,[dic valueForKey:@"Content-Length"]);
}
else
{
//FILE DOESNT EXIST
NSLog(@"Response 2");
completion(FALSE,@"");
}
}
else
{
NSLog(@"Response 3");
completion(FALSE,@"");
}

}];
}

How do I check to see if a file exists on a remote server using shell

Assuming you are using scp and ssh for remote connections something like this should do what you want.

declare -a array1=('user1@user1.user.com');

for i in "${array1[@]}"; do
if ssh -q "$i" "test -f /home/user/directory/file"; then
scp "$i:/home/user/directory/file" /local/path
else
echo 'Could not access remote file.'
fi
done

Alternatively, if you don't necessarily need to care about the difference between the remote file not existing and other possible scp errors then the following would work.

declare -a array1=('user1@user1.user.com');

for i in "${array1[@]}"; do
if ! scp "$i:/home/user/directory/file" /local/path; then
echo 'Remote file did not exist.'
fi
done


Related Topics



Leave a reply



Submit