iOS開發之文件(分段)下載
1、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方法只是返回資源信息,而不會返回數據體
應用場景:
(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); }]; }
4、HTTP 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; }
提示:由于數據是追加的,為了避免重復從網絡下載文件,在下載之前
-
判斷緩存路徑中文件是否已經存在
</li> -
如果存在檢查文件大小
</li> -
如果文件大小與網絡資源大小一致,則不再下載
</li> </ol>
來自:http://my.oschina.net/u/2448717/blog/499781
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!