iOS开发之文件(分段)下载

jopen 8年前

1HTTP 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方法只是返回资源信息,而不会返回数据体

应用场景:

(1) 获取资源Mimetype

(2) 获取资源文件大小,用于端点续传或多线程下载

2、使用块代码获取网络资源大小的方法

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

3确定每次下载数据包的伪代码实现

- (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);      }];  }

4HTTP Range的示例

n  通过设置Range可以指定每次从网路下载数据包的大小

n  Range示例

n  bytes=0-499               从0到499的头500个字节

n  bytes=500-999          从500到999的第二个500字节

n  bytes=500-                 从500字节以后的所有字节

n  bytes=-500                 最后500个字节

n  bytes=500-599,800-899      同时指定几个范围

n  Range小结

n  -   用于分隔

p 前面的数字表示起始字节数

p 后面的数组表示截止字节数,没有表示到末尾

n  用于分组,可以一次指定多个Range,不过很少用

5、分段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);

6分段下载文件

/**NSURLRequestUseProtocolCachePolicy = 0 // 默认的缓存策略,内存缓存   NSURLRequestReloadIgnoringLocalCacheData = 1// 忽略本地的内存缓存*/  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)

7、将数据写入文件

- (void)appendData:(NSData *)data  {  // 打开缓存文件  NSFileHandle *fp =  [NSFileHandle fileHandleForWritingAtPath:self.cachePath];  // 如果文件不存在,直接写入数据  if (!fp) {      [data writeToFile:self.cachePath atomically:YES];  } else {      // 移动到文件末尾      [fp seekToEndOfFile];      // 将数据文件追加到文件末尾      [fp writeData:data];      // 关闭文件句柄      [fp closeFile];  }  }

8检查文件大小

// 判断文件是否存在  if ([[NSFileManager defaultManager] fileExistsAtPath:self.cachePath]) {      NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:self.cachePath error:NULL];      return [dict[NSFileSize] longLongValue];  } else {      return 0;  }

提示:由于数据是追加的,为了避免重复从网络下载文件,在下载之前

  1. 判断缓存路径中文件是否已经存在

  2. 如果存在检查文件大小

  3. 如果文件大小与网络资源大小一致,则不再下载

    来自:http://my.oschina.net/u/2448717/blog/499781