Check if URL-file exist or not

2020-07-24 05:44发布

问题:

I wonder how I can check if a file exist on a server or not, without downloading the data first.

I have around 30 different objects and some of them is connected to a movie on a server. At the moment I use NSData to control if the the URL exist, and then shows the movie, or if it doesn't and then alerts the user that there is no video for that object. The code I use for the moment:

NSString *fPath = [[NSString alloc] initWithFormat:@"http://www.myserver/%@", [rows idNr]];
NSURL *videoURL = [NSURL URLWithString:fPath];
NSData *videoData = [NSData dataWithContentsOfURL:videoURL];

url = [NSURL URLWithString:fPath];
[fPath release];

if (videoData) {
    [self performSelectorOnMainThread:@selector(playVideo:) withObject:url waitUntilDone:NO];
} else {
    NSLog(@"videodata false");
    errorLabel.hidden = NO;
    activityView.hidden = YES;
}

"rows idNr" is the name of the object. This method is doing what I want, but the problem is that with NSData it first "downloading" the file, and when the URL is validated as a file, the movie is loading once again in the movieplayer. This means that it takes twice as long to load the file.

Suggestions?

回答1:

It took me a while to dig out my answer to one of the previous questions on this topic. Quote:

You can use a NSMutableURLRequest to send a HTTP HEAD request (there’s a method called setHTTPMethod). You’ll get the same response headers as with GET, but you won’t have to download the whole resource body. And if you want to get the data synchronously, use the sendSynchronousRequest… method of NSURLConnection.

This way you’ll know if the file exists and won’t download it all if it does.



回答2:

Make an URLConnecion object with desired url request and add NSURLConnectionDelegate into .h file like I want to check "yoururl" is exist or not then you need to do is

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString: @"http://www.google.com"]];

NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];

and then you can track http status code in delegate function of NSURLConnectionDelegate

-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    int code = [(NSHTTPURLResponse *)response statusCode];
    if (code == 404)
    {
        // website not found
        // do your stuff according to your need 
    }
} 

You can also find various status code here.



回答3:

NSError *err;
if ([videoURL checkResourceIsReachableAndReturnError:&err] == NO)
   NSLog(@"wops!");


回答4:

Here's the code for the accepted answer (for your convenience):

How to make call

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
        [request setHTTPMethod:@"HEAD"];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];


回答5:

You could do this by checking the size of the file via an FTP server, using the SIZE command. If the file size is zero then the file simply do not exist.

Check here on how to do this.

You could of course also do this by using a NSURLRequest with NSURLConnection, checking for the status to be either 200 (success) or 404 (failed). The 404 status doesn't have to be that the file doesn't exist though, it could also be that the file just couldn't be retrieved.