检查URL文件是否存在 [英] Check if URL-file exist or not

查看:112
本文介绍了检查URL文件是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在不首先下载数据的情况下检查服务器上是否存在文件.

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

我大约有30个不同的对象,其中一些连接到服务器上的电影.目前,我使用NSData来控制URL是否存在,然后显示电影,或者如果不存在,则警告用户该对象没有视频.我目前使用的代码:

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;
}

行idNr"是对象的名称.这种方法可以满足我的要求,但是问题在于,使用NSData时,它首先下载"了文件,并且当URL被验证为文件时,电影再次在movieplayer中加载.这意味着加载文件所需的时间是原来的两倍.

"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.

建议?

推荐答案

我花了一些时间才找到

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.

这篇关于检查URL文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆