文件下载
文件下载
视频播放
HTTP HEAD方法
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:kTimeout];
request.HTTPMethod = @"HEAD";
[NSURLConnection sendAsynchronousRequest:request queue:self.myQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@"%@", response);
NSLog(@"---------------");
NSLog(@"%@", data);
}];
运行测试代码可以发现,HEAD方法只是返回资源信息,而不会返回数据体
应用场景:
使用块代码获取网络资源大小的方法
- (void)fileSizeWithURL:(NSURL *)url completion:(void (^)(long long contentLength))completion
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:kTimeout];
request.HTTPMethod = @"HEAD";
NSURLResponse *response = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL];
completion(response.expectedContentLength);
}
确定每次下载数据包的伪代码实现
- (void)downloadFileWithURL:(NSURL *)url
{
[self fileSizeWithURL:url completion:^(long long contentLength) {
NSLog(@"文件总大小:%lld", contentLength);
// 根据大小下载文件
while (contentLength > kDownloadBytes) {
NSLog(@"每次下载长度:%lld", (long long)kDownloadBytes);
contentLength -= kDownloadBytes;
}
NSLog(@"最后下载字节数:%lld", contentLength);
}];
}
HTTP Range的示例
通过设置Range可以指定每次从网路下载数据包的大小
Range示例
bytes=0-499 从0到499的头500个字节
bytes=500-999 从500到999的第二个500字节
bytes=500- 从500字节以后的所有字节
bytes=-500 最后500个字节
bytes=500-599,800-899 同时指定几个范围
Range小结
- 用于分隔
, 用于分组,可以一次指定多个Range,不过很少用
分段Range代码实现
long long fromBytes = 0;
long long toBytes = 0;
while (contentLength > kDownloadBytes) {
toBytes = fromBytes + kDownloadBytes - 1;
NSString *range = [NSString stringWithFormat:@"bytes=%lld-%lld", fromBytes, toBytes];
NSLog(@"range %@", range);
fromBytes += kDownloadBytes;
contentLength -= kDownloadBytes;
}
fromBytes = fromBytes + contentLength - 1;
NSString *range = [NSString stringWithFormat:@"bytes=%lld-%lld", fromBytes, toBytes];
NSLog(@"range %@", range);
分段下载文件
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:kTimeout];
NSString *range = [NSString stringWithFormat:@"bytes=%lld-%lld", from, end];
[request setValue:range forHTTPHeaderField:@"Range"];
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL];
NSLog(@"%@-%@-%ld", range, response, (unsigned long)data.length);
提示:
如果GET包含Range请求头,响应会以状态码206(PartialContent)返回而不是200(OK)
将数据写入文件
// 打开缓存文件
NSFileHandle *fp = [NSFileHandle fileHandleForWritingAtPath:self.cachePath];
// 如果文件不存在,直接写入数据
if (!fp) {
[data writeToFile:self.cachePath atomically:YES];
} else {
// 移动到文件末尾
[fp seekToEndOfFile];
// 将数据文件追加到文件末尾
[fp writeData:data];
// 关闭文件句柄
[fp closeFile];
}
检查文件大小
// 判断文件是否存在
if ([[NSFileManager defaultManager] fileExistsAtPath:self.cachePath]) {
NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:self.cachePath error:NULL];
return [dict[NSFileSize] longLongValue];
} else {
return 0;
}
提示:由于数据是追加的,为了避免重复从网络下载文件,在下载之前
Q & A