標籤:
iOS開發中網路請求技術已經是移動app必備技術,而網路中檔案傳輸就是其中重點了。網路檔案傳輸對移動用戶端而言主要分為檔案的上傳和下載。作為開發人員從技術角度會將檔案分為小檔案和大檔案。小檔案因為檔案大小比較小導致傳輸所需時間少傳輸就快,因此不太容易影響使用者體驗,可用的技術就多。而大檔案因為檔案大小比較大導致傳輸時間長,因此就需要考慮到各種使用者體驗,比如避免在上傳下載檔案過程中阻塞主線程影響使用者體驗,就需要使用到多線程技術;為了給方便使用的進度提示,因此又需要開發中跟蹤資料上傳和下載資料的變化;為了提高使用者體驗,也需要考慮到斷點續傳的功能實現;而且大檔案傳輸容易導致資料保持在記憶體中,又需要開發人員處理記憶體中的資料;而為了處理多個檔案或者壓縮傳輸檔案的資料大小,我們開發人員還需要用到壓縮和解壓縮技術。根據不同的需求對大檔案傳輸會有需要用到不同的解決方案,不過多線程斷點續傳是一個理想的在網路中傳輸大檔案的方案。
小檔案下載
小檔案的下載方式比較多,下面列出常用的下載方式:
1.直接用NSData的+ (id)dataWithContentsOfURL:(NSURL *)url方法; 2.利用NSURLConnection發送一個HTTP請求去下載 3.利用NSURLSession發送一個HTTP請求去下載 4.利用AFNetworking發送一個HTTP請求去下載 4.如果是下載圖片,還可以利用SDWebImage架構
大檔案下載最佳解決方案———多線程斷點下載
實現思路:下載開始,建立一個和要下載的檔案大小相同的檔案(如果要下載的檔案為100M,那麼就在沙箱中建立一個100M的檔案,然後計算每一段的下載量,開啟多條線程下載各段的資料,分別寫入對應的檔案部分)。 項目中用到的主要類如下:完成的實現代碼如下:
YYViewController.m
#import "YYViewController.h"#import "YYFileMultiDownloader.h"@interface YYViewController ()@property (nonatomic, strong) YYFileMultiDownloader *fileMultiDownloader;@end@implementation YYViewController- (YYFileMultiDownloader *)fileMultiDownloader{ if (!_fileMultiDownloader) { _fileMultiDownloader = [[YYFileMultiDownloader alloc] init]; // 需要下載的檔案遠程URL _fileMultiDownloader.url = @"http://192.168.1.200:8080/MJServer/resources/jre.zip"; // 檔案儲存到什麼地方 NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; NSString *filepath = [caches stringByAppendingPathComponent:@"jre.zip"]; _fileMultiDownloader.destPath = filepath; } return _fileMultiDownloader;}- (void)viewDidLoad{ [super viewDidLoad]; }- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self.fileMultiDownloader start];}@end
自訂一個基類
YYFileDownloader.h檔案
#import <Foundation/Foundation.h>@interface YYFileDownloader : NSObject{ BOOL _downloading;}/** * 所需要下載檔案的遠程URL(串連伺服器的路徑) */@property (nonatomic, copy) NSString *url;/** * 檔案的儲存路徑(檔案下載到什麼地方) */@property (nonatomic, copy) NSString *destPath;/** * 是否正在下載(有沒有在下載, 只有下載器內部才知道) */@property (nonatomic, readonly, getter = isDownloading) BOOL downloading;/** * 用來監聽下載進度 */@property (nonatomic, copy) void (^progressHandler)(double progress);/** * 開始(恢複)下載 */- (void)start;/** * 暫停下載 */- (void)pause;@end
YYFileDownloader.m檔案
#import "YYFileDownloader.h" @implementation YYFileDownloader@end
下載器類繼承自YYFileDownloader這個類
YYFileSingDownloader.h檔案
#import "YYFileDownloader.h"@interface YYFileSingleDownloader : YYFileDownloader/** * 開始的位置 */@property (nonatomic, assign) long long begin;/** * 結束的位置 */@property (nonatomic, assign) long long end; @end
YYFileSingDownloader.m檔案
#import "YYFileSingleDownloader.h"@interface YYFileSingleDownloader() <NSURLConnectionDataDelegate>/** * 連線物件 */@property (nonatomic, strong) NSURLConnection *conn;/** * 寫資料的檔案控制代碼 */@property (nonatomic, strong) NSFileHandle *writeHandle;/** * 當前已下載資料的長度 */@property (nonatomic, assign) long long currentLength;@end@implementation YYFileSingleDownloader- (NSFileHandle *)writeHandle{ if (!_writeHandle) { _writeHandle = [NSFileHandle fileHandleForWritingAtPath:self.destPath]; } return _writeHandle;}/** * 開始(恢複)下載 */- (void)start{ NSURL *url = [NSURL URLWithString:self.url]; // 預設就是GET請求 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 佈建要求頭資訊 NSString *value = [NSString stringWithFormat:@"bytes=%lld-%lld", self.begin + self.currentLength, self.end]; [request setValue:value forHTTPHeaderField:@"Range"]; self.conn = [NSURLConnection connectionWithRequest:request delegate:self]; _downloading = YES;}/** * 暫停下載 */- (void)pause{ [self.conn cancel]; self.conn = nil; _downloading = NO;}#pragma mark - NSURLConnectionDataDelegate 代理方法/** * 1. 當接受到伺服器的響應(連通了伺服器)就會調用 */- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ }/** * 2. 當接受到伺服器的資料就會調用(可能會被調用多次, 每次調用只會傳遞部分資料) */- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ // 移動到檔案的尾部 [self.writeHandle seekToFileOffset:self.begin + self.currentLength]; // 從當前移動的位置(檔案尾部)開始寫入資料 [self.writeHandle writeData:data]; // 累加長度 self.currentLength += data.length; // 列印下載進度 double progress = (double)self.currentLength / (self.end - self.begin); if (self.progressHandler) { self.progressHandler(progress); }}/** * 3. 當伺服器的資料接受完畢後就會調用 */- (void)connectionDidFinishLoading:(NSURLConnection *)connection{ // 清空屬性值 self.currentLength = 0; // 關閉串連(不再輸入資料到檔案中) [self.writeHandle closeFile]; self.writeHandle = nil;}/** * 請求錯誤(失敗)的時候調用(請求逾時\斷網\沒有網, 一般指用戶端錯誤) */- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ }@end
設計多線程下載器(利用HMFileMultiDownloader能開啟多個線程同時下載一個檔案)
一個多線程下載器只下載一個檔案
YYFileMultiDownloader.h檔案
#import "YYFileDownloader.h" @interface YYFileMultiDownloader : YYFileDownloader @end
YYFileMultiDownloader.m檔案
#import "YYFileMultiDownloader.h"#import "YYFileSingleDownloader.h"#define YYMaxDownloadCount 4@interface YYFileMultiDownloader()@property (nonatomic, strong) NSMutableArray *singleDownloaders;@property (nonatomic, assign) long long totalLength;@end@implementation YYFileMultiDownloader- (void)getFilesize{ NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.url]]; request.HTTPMethod = @"HEAD"; NSURLResponse *response = nil;#warning 這裡要用非同步請求 [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; self.totalLength = response.expectedContentLength;}- (NSMutableArray *)singleDownloaders{ if (!_singleDownloaders) { _singleDownloaders = [NSMutableArray array]; // 獲得檔案大小 [self getFilesize]; // 每條路徑的下載量 long long size = 0; if (self.totalLength % YYMaxDownloadCount == 0) { size = self.totalLength / YYMaxDownloadCount; } else { size = self.totalLength / YYMaxDownloadCount + 1; } // 建立N個下載器 for (int i = 0; i<YYMaxDownloadCount; i++) { YYFileSingleDownloader *singleDownloader = [[YYFileSingleDownloader alloc] init]; singleDownloader.url = self.url; singleDownloader.destPath = self.destPath; singleDownloader.begin = i * size; singleDownloader.end = singleDownloader.begin + size - 1; singleDownloader.progressHandler = ^(double progress){ NSLog(@"%d --- %f", i, progress); }; [_singleDownloaders addObject:singleDownloader]; } // 建立一個跟伺服器檔案等大小的臨時檔案 [[NSFileManager defaultManager] createFileAtPath:self.destPath contents:nil attributes:nil]; // 讓self.destPath檔案的長度是self.totalLengt NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:self.destPath]; [handle truncateFileAtOffset:self.totalLength]; } return _singleDownloaders;}/** * 開始(恢複)下載 */- (void)start{ [self.singleDownloaders makeObjectsPerformSelector:@selector(start)]; _downloading = YES;}/** * 暫停下載 */- (void)pause{ [self.singleDownloaders makeObjectsPerformSelector:@selector(pause)]; _downloading = NO;}@end
補充說明:如何獲得將要下載的檔案的大小?
參考文章:http://www.cnblogs.com/wendingding/p/3947550.html
iOS開發網路請求——大檔案的多線程斷點下載