1. 最簡單的方式
1). 創(chuàng)建一個下載工具類MTDownloader
MTDownloader.h文件
#import <Foundation/Foundation.h>
@interface MTDownloader : NSObject
- (void)download:(NSString *)netUrl;
@end
MTDownloader.m文件
// 下載运沦,傳入下載鏈接
- (void)download:(NSString *)netUrl {
NSURL *url = [NSURL URLWithString:netUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:
^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if (connectionError) {
NSLog(@"連接錯誤 %@", connectionError);
return;
}
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.statusCode == 200 || httpResponse.statusCode == 304) {
// 解析數(shù)據(jù)
// 存在的問題,內(nèi)存暴漲动壤,無法獲取下載進(jìn)度
[data writeToFile:@"/Users/mazaiting/Desktop/1.zip" atomically:YES];
NSLog(@"下載成功");
} else {
NSLog(@"服務(wù)器內(nèi)部錯誤");
}
}];
}
這種下載方式存在的問題是下載時內(nèi)存暴漲盯质,因為文件讀取到內(nèi)存中挺据,并且無法獲取下載進(jìn)度。
2). 在ViewController.m文件中使用
#import "ViewController.h"
#import "MTDownloader.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
// 觸摸屏幕時
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
MTDownloader *downloader = [MTDownloader new];
[downloader download:@"http://127.0.0.1/HBuilder.zip"];
}
@end
2. 解決獲取下載進(jìn)度問題
改造MTDownloader.m文件
#import "MTDownloader.h"
// 分類
@interface MTDownloader () <NSURLConnectionDataDelegate>
// 記錄文件的總長度,因為在獲取下載進(jìn)度的時候需要文件總長度,所以定義為屬性
@property (nonatomic, assign) long long expectedContentLength;
// 記錄當(dāng)前文件的長度
@property (nonatomic, assign) long long currentFileSize;
// 用來記錄總共下載的數(shù)據(jù)
@property (nonatomic, strong) NSMutableData *mutableData;
@end
@implementation MTDownloader
// 懶加載
- (NSMutableData *)mutableData {
if (_mutableData == nil) {
_mutableData = [NSMutableData data];
}
return _mutableData;
}
- (void)download:(NSString *)netUrl {
// 創(chuàng)建url
NSURL *url = [NSURL URLWithString:netUrl];
// 創(chuàng)建下載請求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 設(shè)置代理, 實現(xiàn)NSURLConnectionDataDelegate協(xié)議
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
#pragma mark- 代理方法
// 接收到響應(yīng)頭
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"接收到響應(yīng) %@", response);
// 獲取到文件的總長度
self.expectedContentLength = response.expectedContentLength;
}
// 接收到數(shù)據(jù)--一點點接收
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// 將每次接收到的字節(jié)長度與已有的文件長度相加
self.currentFileSize += data.length;
// 添加接收的數(shù)據(jù)
[self.mutableData appendData:data];
// 當(dāng)前已下載的文件長度 * 1.0 / 文件的總長度 = 當(dāng)前的進(jìn)度
float process = self.currentFileSize * 1.0 / self.expectedContentLength;
NSLog(@"接收數(shù)據(jù): %f", process);
}
// 下載完成
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"下載完成");
// 下載完成后將字節(jié)數(shù)組寫入文件
[self.mutableData writeToFile:@"/Users/mazaiting/Desktop/1.zip" atomically:YES];
}
// 下載出錯
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"下載出錯");
}
@end
改造完成后蒸其,可以獲取下載文件的進(jìn)度,但仍然后出現(xiàn)內(nèi)存暴漲的問題库快。
3. 解決文件內(nèi)存暴漲--NSFileHandle/NSOutputStream
將第二步中記錄下載數(shù)據(jù)的mutableData屬性刪除摸袁,在此處不再需要,因為是將文件一點一點的寫入义屏。
在接收一點點接收數(shù)據(jù)的地方進(jìn)行數(shù)據(jù)的存儲靠汁。
1). NSFileHandle解決
#import "MTDownloader.h"
// 分類
@interface MTDownloader () <NSURLConnectionDataDelegate>
// 記錄文件的總長度,因為在獲取下載進(jìn)度的時候需要文件總長度,所以定義為屬性
@property (nonatomic, assign) long long expectedContentLength;
// 記錄當(dāng)前文件的長度
@property (nonatomic, assign) long long currentFileSize;
@end
@implementation MTDownloader
- (void)download:(NSString *)netUrl {
// 創(chuàng)建url
NSURL *url = [NSURL URLWithString:netUrl];
// 創(chuàng)建下載請求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 設(shè)置代理, 實現(xiàn)NSURLConnectionDataDelegate協(xié)議
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
#pragma mark- 代理方法
// 接收到響應(yīng)頭
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"接收到響應(yīng) %@", response);
// 獲取到文件的總長度
self.expectedContentLength = response.expectedContentLength;
}
// 接收到數(shù)據(jù)--一點點接收
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// 將每次接收到的字節(jié)長度與已有的文件長度相加
self.currentFileSize += data.length;
// 當(dāng)前已下載的文件長度 * 1.0 / 文件的總長度 = 當(dāng)前的進(jìn)度
float process = self.currentFileSize * 1.0 / self.expectedContentLength;
NSLog(@"接收數(shù)據(jù): %f", process);
// 保存數(shù)據(jù)
[self saveData:data];
}
// 下載完成
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"下載完成");
}
// 下載出錯
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"下載出錯");
}
// 保存數(shù)據(jù)
// data 每次下載的二進(jìn)制數(shù)據(jù)
- (void)saveData:(NSData *)data {
// 保存文件的路徑
NSString *filePath = @"/Users/mazaiting/Desktop/1.zip";
// 根據(jù)路徑獲取NSFileHandle, 它不會自動創(chuàng)建文件闽铐,而是第0個字節(jié)添加數(shù)據(jù)
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
// 如果fileHandle存在蝶怔,則讓fileHandle的offset偏移量指向文件的末尾,進(jìn)行數(shù)據(jù)的添加
// 如果fileHandle不存在兄墅,則創(chuàng)建文件
if (fileHandle) {
// fileHandle存在
// 將文件指針指向文件的末尾
[fileHandle seekToEndOfFile];
// 寫數(shù)據(jù)
[fileHandle writeData:data];
// 關(guān)閉文件
[fileHandle closeFile];
} else {
// fileHandle不存在
[data writeToFile:filePath atomically:YES];
}
}
@end
2). NSOutputStream解決
流操作時要注意打開流踢星,關(guān)閉流,并且要注意初始化流的位置隙咸。文件寫入時推薦使用流操作沐悦。
#import "MTDownloader.h"
// 分類
@interface MTDownloader () <NSURLConnectionDataDelegate>
// 記錄文件的總長度,因為在獲取下載進(jìn)度的時候需要文件總長度,所以定義為屬性
@property (nonatomic, assign) long long expectedContentLength;
// 記錄當(dāng)前文件的長度
@property (nonatomic, assign) long long currentFileSize;
// NSOutputStream用來保存每次下載的二進(jìn)制數(shù)據(jù)
@property (nonatomic, strong) NSOutputStream *outputStream;
@end
@implementation MTDownloader
- (void)download:(NSString *)netUrl {
// 創(chuàng)建url
NSURL *url = [NSURL URLWithString:netUrl];
// 創(chuàng)建下載請求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 設(shè)置代理, 實現(xiàn)NSURLConnectionDataDelegate協(xié)議
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
#pragma mark- 代理方法
// 接收到響應(yīng)頭
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"接收到響應(yīng) %@", response);
// 獲取到文件的總長度
self.expectedContentLength = response.expectedContentLength;
// 保存文件的路徑
NSString *filePath = @"/Users/mazaiting/Desktop/1.zip";
// 接收到響應(yīng)頭的時候初始化outputStream
self.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:YES];
// 打開流
[self.outputStream open];
}
// 接收到數(shù)據(jù)--一點點接收
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// 將每次接收到的字節(jié)長度與已有的文件長度相加
self.currentFileSize += data.length;
// 當(dāng)前已下載的文件長度 * 1.0 / 文件的總長度 = 當(dāng)前的進(jìn)度
float process = self.currentFileSize * 1.0 / self.expectedContentLength;
NSLog(@"接收數(shù)據(jù): %f", process);
// 保存數(shù)據(jù)
[self saveData:data];
}
// 下載完成
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"下載完成");
// 下載完成的時候扎瓶,關(guān)閉流
[self.outputStream close];}
// 下載出錯
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"下載出錯");
// 判斷流是否為空所踊,下載出錯的時候,關(guān)閉流
if (self.outputStream) {
[self.outputStream close];
}
}
// NSOutputStream保存數(shù)據(jù)
// data 每次下載的二進(jìn)制文件
- (void)saveData:(NSData *)data {
// 寫數(shù)據(jù)
[self.outputStream write:data.bytes maxLength:data.length];
}
// NSFileHandle保存數(shù)據(jù)
// data 每次下載的二進(jìn)制數(shù)據(jù)
- (void)saveData1:(NSData *)data {
// 保存文件的路徑
NSString *filePath = @"/Users/mazaiting/Desktop/1.zip";
// 根據(jù)路徑獲取NSFileHandle, 它不會自動創(chuàng)建文件概荷,而是第0個字節(jié)添加數(shù)據(jù)
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
// 如果fileHandle存在秕岛,則讓fileHandle的offset偏移量指向文件的末尾,進(jìn)行數(shù)據(jù)的添加
// 如果fileHandle不存在误证,則創(chuàng)建文件
if (fileHandle) {
// fileHandle存在
// 將文件指針指向文件的末尾
[fileHandle seekToEndOfFile];
// 寫數(shù)據(jù)
[fileHandle writeData:data];
// 關(guān)閉文件
[fileHandle closeFile];
} else {
// fileHandle不存在
[data writeToFile:filePath atomically:YES];
}
}
@end
此時继薛,一個下載的操作工具類就已完成,但目前無法使用斷點下載愈捅。
4. 文件的斷點下載操作
斷點續(xù)傳思路
I. 首先發(fā)送一個head請求遏考,獲取到服務(wù)器文件的大小和名稱
II. 判斷本地文件是否存在
1). 不存在,從0開始下載
2). 存在
a. 本地文件大小 == 服務(wù)器文件大小 不需要下載
b. 本地文件大小 < 服務(wù)器文件大小 從當(dāng)前位置下載
c. 本地文件打下 > 服務(wù)器文件大小 刪除文件蓝谨,從0下載
//
// 斷點續(xù)傳思路
// 1. 首先發(fā)送一個head請求灌具,獲取到服務(wù)器文件的大小和名稱
// 2. 判斷本地文件是否存在
// 1). 不存在青团,從0開始下載
// 2). 存在
// a. 本地文件大小 == 服務(wù)器文件大小 不需要下載
// b. 本地文件大小 < 服務(wù)器文件大小 從當(dāng)前位置下載
// c. 本地文件打下 > 服務(wù)器文件大小 刪除文件,從0下載
#import "MTDownloader.h"
// 分類
@interface MTDownloader () <NSURLConnectionDataDelegate>
// 記錄文件的總長度,因為在獲取下載進(jìn)度的時候需要文件總長度咖楣,所以定義為屬性
@property (nonatomic, assign) long long expectedContentLength;
// 記錄當(dāng)前文件的長度
@property (nonatomic, assign) long long currentFileSize;
// NSOutputStream用來保存每次下載的二進(jìn)制數(shù)據(jù)
@property (nonatomic, strong) NSOutputStream *outputStream;
// 文件的存儲路徑
@property (nonatomic, strong) NSString *filePath;
@end
@implementation MTDownloader
- (void)download:(NSString *)netUrl {
// 創(chuàng)建url
NSURL *url = [NSURL URLWithString:netUrl];
// 獲取服務(wù)器文件信息, 獲取當(dāng)前文件長度
[self getServerFileInfo:url];
// 獲取本地文件信息督笆,并且和服務(wù)器做比較
self.currentFileSize = [self getLocalFileInfo];
// 判斷當(dāng)前文件大小是否是-1,如果是-1诱贿,說明文件已經(jīng)下載完成
if (self.currentFileSize == -1) {
NSLog(@"文件已下載完成娃肿,請不要重復(fù)下載!!!");
return;
}
// 從指定位置處下載文件
[self downloadFile: url];
}
// 從指定位置處下載文件
- (void)downloadFile:(NSURL *)url {
// 創(chuàng)建一個請求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 請求頭
// Range:bytes=x-y 從x個字節(jié)下載到y(tǒng)個字節(jié)
// Range:bytes=x- 從x個字節(jié)下載到最后
// Range:bytes=-y 從開始下載到y(tǒng)個字節(jié)
[request setValue:[NSString stringWithFormat:@"bytes=%lld-", self.currentFileSize] forHTTPHeaderField:@"Range"];
// 設(shè)置代理, 實現(xiàn)NSURLConnectionDataDelegate協(xié)議
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
// 檢測本地文件大小
- (long long)getLocalFileInfo {
// 文件的長度, 如果文件不存在珠十,則為0
long long fileSize = 0;
// 獲取文件管理者對象
NSFileManager *fileManager = [NSFileManager defaultManager];
// 判斷文件是否存在
if ([fileManager fileExistsAtPath:self.filePath]) {
// 文件存在
// 獲取本地文件的大小
NSDictionary *attrs = [fileManager attributesOfItemAtPath:self.filePath error:NULL];
// 賦值
fileSize = attrs.fileSize;
// 文件大小等于服務(wù)器文件長度
if (fileSize == self.expectedContentLength) {
fileSize = -1;
}
// 文件大小大于服務(wù)器文件長度
if (fileSize > self.expectedContentLength) {
fileSize = 0;
// 刪除文件
[fileManager removeItemAtPath:self.filePath error:NULL];
}
}
return fileSize;
}
// 獲取服務(wù)器文件信息
// 文件的url
- (void)getServerFileInfo:(NSURL *)url {
// 創(chuàng)建一個請求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 設(shè)置請求方法
request.HTTPMethod = @"head";
// 同步請求響應(yīng)體
NSURLResponse *response = nil;
// 發(fā)送一個同步請求
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL];
// 設(shè)置文件的總長度料扰,注意:在下載時- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;方法中為當(dāng)前文件總長度賦值的語句要刪除
self.expectedContentLength = response.expectedContentLength;
// 設(shè)置文件的存儲路徑, 存儲在臨時目錄下,拼接文件名
self.filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:response.suggestedFilename];
NSLog(@"%@", self.filePath);
}
#pragma mark- 代理方法
// 接收到響應(yīng)頭
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"接收到響應(yīng) %@", response);
// 接收到響應(yīng)頭的時候初始化outputStream
self.outputStream = [NSOutputStream outputStreamToFileAtPath:self.filePath append:YES];
// 打開流
[self.outputStream open];
}
// 接收到數(shù)據(jù)--一點點接收
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// 將每次接收到的字節(jié)長度與已有的文件長度相加
self.currentFileSize += data.length;
// 當(dāng)前已下載的文件長度 * 1.0 / 文件的總長度 = 當(dāng)前的進(jìn)度
float process = self.currentFileSize * 1.0 / self.expectedContentLength;
NSLog(@"接收數(shù)據(jù): %f", process);
// 保存數(shù)據(jù)
[self saveData:data];
}
// 下載完成
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"下載完成");
// 下載完成的時候焙蹭,關(guān)閉流
[self.outputStream close];}
// 下載出錯
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"下載出錯");
// 判斷流是否為空晒杈,下載出錯的時候,關(guān)閉流
if (self.outputStream) {
[self.outputStream close];
}
}
// NSOutputStream保存數(shù)據(jù)
// data 每次下載的二進(jìn)制文件
- (void)saveData:(NSData *)data {
// 寫數(shù)據(jù)
[self.outputStream write:data.bytes maxLength:data.length];
}
// NSFileHandle保存數(shù)據(jù)
// data 每次下載的二進(jìn)制數(shù)據(jù)
- (void)saveData1:(NSData *)data {
// 保存文件的路徑
NSString *filePath = @"/Users/mazaiting/Desktop/1.zip";
// 根據(jù)路徑獲取NSFileHandle, 它不會自動創(chuàng)建文件壳嚎,而是第0個字節(jié)添加數(shù)據(jù)
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
// 如果fileHandle存在桐智,則讓fileHandle的offset偏移量指向文件的末尾,進(jìn)行數(shù)據(jù)的添加
// 如果fileHandle不存在烟馅,則創(chuàng)建文件
if (fileHandle) {
// fileHandle存在
// 將文件指針指向文件的末尾
[fileHandle seekToEndOfFile];
// 寫數(shù)據(jù)
[fileHandle writeData:data];
// 關(guān)閉文件
[fileHandle closeFile];
} else {
// fileHandle不存在
[data writeToFile:filePath atomically:YES];
}
}
@end
5. 下載回調(diào)設(shè)置
MTDownloader.h 中在download方法中增加成功、進(jìn)度然磷、失敗的對調(diào)block郑趁。
#import <Foundation/Foundation.h>
@interface MTDownloader : NSObject
// 下載方法
// netUrl 下載的網(wǎng)絡(luò)連接
// successBlock 成功回調(diào)block
// processBlock 進(jìn)度對調(diào)block
// errorBlock 異常回調(diào)block
- (void)download:(NSString *)netUrl successBlock:(void(^)(NSString *path))successBlock processBlock:(void(^)(float process))processBlock errorBlock:(void(^)(NSError *error))errorBlock;
@end
MTDownloader.m 中實現(xiàn)該方法姿搜,并將下載文件的操作重新開啟一個新的線程來執(zhí)行下載任務(wù)寡润。
//
// 斷點續(xù)傳思路
// 1. 首先發(fā)送一個head請求,獲取到服務(wù)器文件的大小和名稱
// 2. 判斷本地文件是否存在
// 1). 不存在舅柜,從0開始下載
// 2). 存在
// a. 本地文件大小 == 服務(wù)器文件大小 不需要下載
// b. 本地文件大小 < 服務(wù)器文件大小 從當(dāng)前位置下載
// c. 本地文件打下 > 服務(wù)器文件大小 刪除文件梭纹,從0下載
#import "MTDownloader.h"
// 分類
@interface MTDownloader () <NSURLConnectionDataDelegate>
// 記錄文件的總長度,因為在獲取下載進(jìn)度的時候需要文件總長度,所以定義為屬性
@property (nonatomic, assign) long long expectedContentLength;
// 記錄當(dāng)前文件的長度
@property (nonatomic, assign) long long currentFileSize;
// NSOutputStream用來保存每次下載的二進(jìn)制數(shù)據(jù)
@property (nonatomic, strong) NSOutputStream *outputStream;
// 文件的存儲路徑
@property (nonatomic, strong) NSString *filePath;
// 下載成功后的回調(diào)
@property (nonatomic, copy) void (^successBlock)(NSString *path);
// 下載進(jìn)度回調(diào)
@property (nonatomic, copy) void (^processBlock)(float process);
// 下載異常的回調(diào)
@property (nonatomic, copy) void (^errorBlock)(NSError *error);
@end
@implementation MTDownloader
// 下載
- (void)download:(NSString *)netUrl successBlock:(void (^)(NSString *))successBlock processBlock:(void (^)(float))processBlock errorBlock:(void (^)(NSError *))errorBlock {
self.successBlock = successBlock;
self.processBlock = processBlock;
self.errorBlock = errorBlock;
// 創(chuàng)建url
NSURL *url = [NSURL URLWithString:netUrl];
// 獲取服務(wù)器文件信息, 獲取當(dāng)前文件長度
[self getServerFileInfo:url];
// 獲取本地文件信息致份,并且和服務(wù)器做比較
self.currentFileSize = [self getLocalFileInfo];
// 判斷當(dāng)前文件大小是否是-1变抽,如果是-1,說明文件已經(jīng)下載完成
if (self.currentFileSize == -1) {
// NSLog(@"文件已下載完成氮块,請不要重復(fù)下載!!!");
// 如果下載回調(diào)不為空
if (self.successBlock) {
// 分發(fā)消息到主線程绍载,讓主線程來處理
dispatch_async(dispatch_get_main_queue(), ^{
self.successBlock(self.filePath);
});
}
return;
}
// 從指定位置處下載文件
[self downloadFile: url];
}
// 從指定位置處下載文件
- (void)downloadFile:(NSURL *)url {
// 創(chuàng)建一個新的線程,并添加到隊列中
[[NSOperationQueue new] addOperationWithBlock:^{
// 創(chuàng)建一個請求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 請求頭
// Range:bytes=x-y 從x個字節(jié)下載到y(tǒng)個字節(jié)
// Range:bytes=x- 從x個字節(jié)下載到最后
// Range:bytes=-y 從開始下載到y(tǒng)個字節(jié)
[request setValue:[NSString stringWithFormat:@"bytes=%lld-", self.currentFileSize] forHTTPHeaderField:@"Range"];
// 設(shè)置代理, 實現(xiàn)NSURLConnectionDataDelegate協(xié)議
[[NSURLConnection alloc] initWithRequest:request delegate:self];
// 開啟消息循環(huán)
[[NSRunLoop currentRunLoop] run];
}];
}
// 檢測本地文件大小
- (long long)getLocalFileInfo {
// 文件的長度滔蝉, 如果文件不存在击儡,則為0
long long fileSize = 0;
// 獲取文件管理者對象
NSFileManager *fileManager = [NSFileManager defaultManager];
// 判斷文件是否存在
if ([fileManager fileExistsAtPath:self.filePath]) {
// 文件存在
// 獲取本地文件的大小
NSDictionary *attrs = [fileManager attributesOfItemAtPath:self.filePath error:NULL];
// 賦值
fileSize = attrs.fileSize;
// 文件大小等于服務(wù)器文件長度
if (fileSize == self.expectedContentLength) {
fileSize = -1;
}
// 文件大小大于服務(wù)器文件長度
if (fileSize > self.expectedContentLength) {
fileSize = 0;
// 刪除文件
[fileManager removeItemAtPath:self.filePath error:NULL];
}
}
return fileSize;
}
// 獲取服務(wù)器文件信息
// 文件的url
- (void)getServerFileInfo:(NSURL *)url {
// 創(chuàng)建一個請求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 設(shè)置請求方法
request.HTTPMethod = @"head";
// 同步請求響應(yīng)體
NSURLResponse *response = nil;
// 發(fā)送一個同步請求
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL];
// 設(shè)置文件的總長度,注意:在下載時- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;方法中為當(dāng)前文件總長度賦值的語句要刪除
self.expectedContentLength = response.expectedContentLength;
// 設(shè)置文件的存儲路徑, 存儲在臨時目錄下蝠引,拼接文件名
self.filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:response.suggestedFilename];
// NSLog(@"%@", self.filePath);
}
#pragma mark- 代理方法
// 接收到響應(yīng)頭
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// NSLog(@"接收到響應(yīng) %@", response);
// 接收到響應(yīng)頭的時候初始化outputStream
self.outputStream = [NSOutputStream outputStreamToFileAtPath:self.filePath append:YES];
// 打開流
[self.outputStream open];
}
// 接收到數(shù)據(jù)--一點點接收
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// 將每次接收到的字節(jié)長度與已有的文件長度相加
self.currentFileSize += data.length;
// 當(dāng)前已下載的文件長度 * 1.0 / 文件的總長度 = 當(dāng)前的進(jìn)度
float process = self.currentFileSize * 1.0 / self.expectedContentLength;
// NSLog(@"接收數(shù)據(jù): %f", process);
// 保存數(shù)據(jù)
[self saveData:data];
// 判斷進(jìn)度回調(diào)是否為空
if (self.processBlock) {
// 如果不為空則回到主線程
dispatch_async(dispatch_get_main_queue(), ^{
self.processBlock(process);
});
}
}
// 下載完成
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// NSLog(@"下載完成");
// 下載完成的時候阳谍,關(guān)閉流
[self.outputStream close];
// 如果下載回調(diào)不為空
if (self.successBlock) {
// 分發(fā)消息到主線程蛀柴,讓主線程來處理
dispatch_async(dispatch_get_main_queue(), ^{
self.successBlock(self.filePath);
});
}
}
// 下載出錯
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// NSLog(@"下載出錯");
// 判斷流是否為空,下載出錯的時候矫夯,關(guān)閉流
if (self.outputStream) {
[self.outputStream close];
}
// 判斷異趁福回調(diào)是否為空
if (self.errorBlock) {
// 不為空回到主線程
dispatch_async(dispatch_get_main_queue(), ^{
self.errorBlock(error);
});
}
}
// NSOutputStream保存數(shù)據(jù)
// data 每次下載的二進(jìn)制文件
- (void)saveData:(NSData *)data {
// 寫數(shù)據(jù)
[self.outputStream write:data.bytes maxLength:data.length];
}
// NSFileHandle保存數(shù)據(jù)
// data 每次下載的二進(jìn)制數(shù)據(jù)
- (void)saveData1:(NSData *)data {
// 保存文件的路徑
NSString *filePath = @"/Users/mazaiting/Desktop/1.zip";
// 根據(jù)路徑獲取NSFileHandle, 它不會自動創(chuàng)建文件,而是第0個字節(jié)添加數(shù)據(jù)
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
// 如果fileHandle存在茧痒,則讓fileHandle的offset偏移量指向文件的末尾肮韧,進(jìn)行數(shù)據(jù)的添加
// 如果fileHandle不存在,則創(chuàng)建文件
if (fileHandle) {
// fileHandle存在
// 將文件指針指向文件的末尾
[fileHandle seekToEndOfFile];
// 寫數(shù)據(jù)
[fileHandle writeData:data];
// 關(guān)閉文件
[fileHandle closeFile];
} else {
// fileHandle不存在
[data writeToFile:filePath atomically:YES];
}
}
@end
ViewController.m中的調(diào)用
#import "ViewController.h"
#import "MTDownloader.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
MTDownloader *downloader = [MTDownloader new];
NSString *url = @"http://127.0.0.1/HBuilder.zip";
[downloader download:url successBlock:^(NSString *path) {
NSLog(@"下載成功");
} processBlock:^(float process) {
NSLog(@"下載進(jìn)度: %f", process);
} errorBlock:^(NSError *error) {
NSLog(@"下載出錯 %@", error);
}];
}
@end
6. 進(jìn)度條顯示--- 自定義圓形進(jìn)度條
#import "ViewController.h"
#import "MTDownloader.h"
#import "MTProcessView.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet MTProcessView *processView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
MTDownloader *downloader = [MTDownloader new];
NSString *url = @"http://127.0.0.1/HBuilder.zip";
[downloader download:url successBlock:^(NSString *path) {
NSLog(@"下載成功");
} processBlock:^(float process) {
self.processView.process = process;
NSLog(@"下載進(jìn)度: %f", process);
} errorBlock:^(NSError *error) {
NSLog(@"下載出錯 %@", error);
}];
}
@end
7. 暫停下載
在MTDownloader.h中添加暫停下載方法
#import <Foundation/Foundation.h>
@interface MTDownloader : NSObject
// ... 省略其他方法
// 暫停下載
- (void)pause;
@end
在MTDownloader.m中實現(xiàn)該方法旺订, 以下內(nèi)容為該類修改的方法弄企。
#import "MTDownloader.h"
// 分類
@interface MTDownloader () <NSURLConnectionDataDelegate>
// 下載時創(chuàng)建的NSURLConnection,可用于取消下載
@property (nonatomic, strong) NSURLConnection *urlConnection;
@end
@implementation MTDownloader
// 從指定位置處下載文件
- (void)downloadFile:(NSURL *)url {
// 創(chuàng)建一個新的線程区拳,并添加到隊列中
[[NSOperationQueue new] addOperationWithBlock:^{
// 創(chuàng)建一個請求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 請求頭
// Range:bytes=x-y 從x個字節(jié)下載到y(tǒng)個字節(jié)
// Range:bytes=x- 從x個字節(jié)下載到最后
// Range:bytes=-y 從開始下載到y(tǒng)個字節(jié)
[request setValue:[NSString stringWithFormat:@"bytes=%lld-", self.currentFileSize] forHTTPHeaderField:@"Range"];
// 設(shè)置代理, 實現(xiàn)NSURLConnectionDataDelegate協(xié)議
self.urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
// 開啟消息循環(huán)
[[NSRunLoop currentRunLoop] run];
}];
}
// 暫停下載
- (void)pause {
// 取消下載
[self.urlConnection cancel];
}
8. 下載類的封裝
MTDownloadManager.h
#import <Foundation/Foundation.h>
@interface MTDownloadManager : NSObject
// 單例
+ (instancetype)sharedManager;
// 下載方法
// netUrl 下載的網(wǎng)絡(luò)連接
// successBlock 成功回調(diào)block
// processBlock 進(jìn)度對調(diào)block
// errorBlock 異尘辛欤回調(diào)block
- (void)download:(NSString *)netUrl successBlock:(void(^)(NSString *path))successBlock processBlock:(void(^)(float process))processBlock errorBlock:(void(^)(NSError *error))errorBlock;
// 暫停下載
- (void)pause:(NSString *)netUrl;
@end
MTDownloadManager.m
#import "MTDownloadManager.h"
#import "MTDownloader.h"
@interface MTDownloadManager ()
// 下載操作緩存池
@property (nonatomic, strong) NSMutableDictionary *downloadCache;
@end
@implementation MTDownloadManager
// 單例
+ (instancetype)sharedManager {
static id instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [self new];
});
return instance;
}
// 懶加載
- (NSMutableDictionary *)downloadCache {
if (_downloadCache == nil) {
_downloadCache = [NSMutableDictionary dictionaryWithCapacity:10];
}
return _downloadCache;
}
// 下載
- (void)download:(NSString *)netUrl successBlock:(void (^)(NSString *))successBlock processBlock:(void (^)(float))processBlock errorBlock:(void (^)(NSError *))errorBlock {
// 判斷緩存池中是否有下載操作
if (self.downloadCache[netUrl]) {
NSLog(@"正在拼命下載中...");
return;
}
// 新建下載
MTDownloader *downloader = [MTDownloader download:netUrl successBlock:^(NSString *path) {
// 移除下載操作
[self.downloadCache removeObjectForKey:netUrl];
if (successBlock) {
successBlock(path);
}
} processBlock:processBlock errorBlock:^(NSError *error) {
// 移除下載操作
[self.downloadCache removeObjectForKey:netUrl];
if (errorBlock) {
errorBlock(error);
}
}];
[[NSOperationQueue new] addOperation:downloader];
// 添加到緩存池中
[self.downloadCache setObject:downloader forKey:netUrl];
}
// 暫停
- (void)pause:(NSString *)netUrl {
MTDownloader *downloader = self.downloadCache[netUrl];
// 判斷是否為空,如果不為空樱调,則暫停约素;如果為空,則什么都不做
if (downloader == nil) {
NSLog(@"沒有下載操作笆凌,無法暫停");
return;
}
[downloader pause];
// 刪除緩存池中的下載操作
[self.downloadCache removeObjectForKey:netUrl];
}
@end
MTDownloader.h
#import <Foundation/Foundation.h>
@interface MTDownloader : NSOperation
// 下載方法
// netUrl 下載的網(wǎng)絡(luò)連接
// successBlock 成功回調(diào)block
// processBlock 進(jìn)度對調(diào)block
// errorBlock 異呈チ裕回調(diào)block
+ (instancetype)download:(NSString *)netUrl successBlock:(void(^)(NSString *path))successBlock processBlock:(void(^)(float process))processBlock errorBlock:(void(^)(NSError *error))errorBlock;
// 暫停下載
- (void)pause;
@end
MTDownloader.m
// 斷點續(xù)傳思路
// 1. 首先發(fā)送一個head請求,獲取到服務(wù)器文件的大小和名稱
// 2. 判斷本地文件是否存在
// 1). 不存在乞而,從0開始下載
// 2). 存在
// a. 本地文件大小 == 服務(wù)器文件大小 不需要下載
// b. 本地文件大小 < 服務(wù)器文件大小 從當(dāng)前位置下載
// c. 本地文件打下 > 服務(wù)器文件大小 刪除文件送悔,從0下載
#import "MTDownloader.h"
// 分類
@interface MTDownloader () <NSURLConnectionDataDelegate>
// 記錄文件的總長度,因為在獲取下載進(jìn)度的時候需要文件總長度,所以定義為屬性
@property (nonatomic, assign) long long expectedContentLength;
// 記錄當(dāng)前文件的長度
@property (nonatomic, assign) long long currentFileSize;
// NSOutputStream用來保存每次下載的二進(jìn)制數(shù)據(jù)
@property (nonatomic, strong) NSOutputStream *outputStream;
// 下載時創(chuàng)建的NSURLConnection爪模,可用于取消下載
@property (nonatomic, strong) NSURLConnection *urlConnection;
// 文件的存儲路徑
@property (nonatomic, strong) NSString *filePath;
// 下載成功后的回調(diào)
@property (nonatomic, copy) void (^successBlock)(NSString *path);
// 下載進(jìn)度回調(diào)
@property (nonatomic, copy) void (^processBlock)(float process);
// 下載異常的回調(diào)
@property (nonatomic, copy) void (^errorBlock)(NSError *error);
// 下載鏈接
@property (nonatomic, copy) NSString *urlString;
@end
@implementation MTDownloader
// 下載
+ (instancetype)download:(NSString *)netUrl successBlock:(void (^)(NSString *))successBlock processBlock:(void (^)(float))processBlock errorBlock:(void (^)(NSError *))errorBlock {
MTDownloader *downloader = [MTDownloader new];
downloader.successBlock = successBlock;
downloader.processBlock = processBlock;
downloader.errorBlock = errorBlock;
downloader.urlString = netUrl;
return downloader;
}
- (void)main {
@autoreleasepool {
// 創(chuàng)建url
NSURL *url = [NSURL URLWithString:self.urlString];
// 獲取服務(wù)器文件信息, 獲取當(dāng)前文件長度
[self getServerFileInfo:url];
// 獲取本地文件信息欠啤,并且和服務(wù)器做比較
self.currentFileSize = [self getLocalFileInfo];
// 判斷當(dāng)前文件大小是否是-1,如果是-1屋灌,說明文件已經(jīng)下載完成
if (self.currentFileSize == -1) {
// NSLog(@"文件已下載完成洁段,請不要重復(fù)下載!!!");
// 如果下載回調(diào)不為空
if (self.successBlock) {
// 分發(fā)消息到主線程,讓主線程來處理
dispatch_async(dispatch_get_main_queue(), ^{
self.successBlock(self.filePath);
});
}
return;
}
// 從指定位置處下載文件
[self downloadFile: url];
}
}
// 從指定位置處下載文件
- (void)downloadFile:(NSURL *)url {
// 創(chuàng)建一個新的線程共郭,并添加到隊列中
[[NSOperationQueue new] addOperationWithBlock:^{
// 創(chuàng)建一個請求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 請求頭
// Range:bytes=x-y 從x個字節(jié)下載到y(tǒng)個字節(jié)
// Range:bytes=x- 從x個字節(jié)下載到最后
// Range:bytes=-y 從開始下載到y(tǒng)個字節(jié)
[request setValue:[NSString stringWithFormat:@"bytes=%lld-", self.currentFileSize] forHTTPHeaderField:@"Range"];
// 設(shè)置代理, 實現(xiàn)NSURLConnectionDataDelegate協(xié)議
self.urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
// 開啟消息循環(huán)
[[NSRunLoop currentRunLoop] run];
}];
}
// 檢測本地文件大小
- (long long)getLocalFileInfo {
// 文件的長度祠丝, 如果文件不存在,則為0
long long fileSize = 0;
// 獲取文件管理者對象
NSFileManager *fileManager = [NSFileManager defaultManager];
// 判斷文件是否存在
if ([fileManager fileExistsAtPath:self.filePath]) {
// 文件存在
// 獲取本地文件的大小
NSDictionary *attrs = [fileManager attributesOfItemAtPath:self.filePath error:NULL];
// 賦值
fileSize = attrs.fileSize;
// 文件大小等于服務(wù)器文件長度
if (fileSize == self.expectedContentLength) {
fileSize = -1;
}
// 文件大小大于服務(wù)器文件長度
if (fileSize > self.expectedContentLength) {
fileSize = 0;
// 刪除文件
[fileManager removeItemAtPath:self.filePath error:NULL];
}
}
return fileSize;
}
// 獲取服務(wù)器文件信息
// 文件的url
- (void)getServerFileInfo:(NSURL *)url {
// 創(chuàng)建一個請求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 設(shè)置請求方法
request.HTTPMethod = @"head";
// 同步請求響應(yīng)體
NSURLResponse *response = nil;
// 發(fā)送一個同步請求
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL];
// 設(shè)置文件的總長度落塑,注意:在下載時- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;方法中為當(dāng)前文件總長度賦值的語句要刪除
self.expectedContentLength = response.expectedContentLength;
// 設(shè)置文件的存儲路徑, 存儲在臨時目錄下纽疟,拼接文件名
self.filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:response.suggestedFilename];
// NSLog(@"%@", self.filePath);
}
// 暫停下載
- (void)pause {
// 取消下載
[self.urlConnection cancel];
// 取消自定義操作
[self cancel];
}
#pragma mark- 代理方法
// 接收到響應(yīng)頭
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// NSLog(@"接收到響應(yīng) %@", response);
// 接收到響應(yīng)頭的時候初始化outputStream
self.outputStream = [NSOutputStream outputStreamToFileAtPath:self.filePath append:YES];
// 打開流
[self.outputStream open];
}
// 接收到數(shù)據(jù)--一點點接收
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// 將每次接收到的字節(jié)長度與已有的文件長度相加
self.currentFileSize += data.length;
// 當(dāng)前已下載的文件長度 * 1.0 / 文件的總長度 = 當(dāng)前的進(jìn)度
float process = self.currentFileSize * 1.0 / self.expectedContentLength;
// NSLog(@"接收數(shù)據(jù): %f", process);
// 保存數(shù)據(jù)
[self saveData:data];
// 判斷進(jìn)度回調(diào)是否為空
if (self.processBlock) {
// 如果不為空則回到主線程
dispatch_async(dispatch_get_main_queue(), ^{
self.processBlock(process);
});
}
}
// 下載完成
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// NSLog(@"下載完成");
// 下載完成的時候,關(guān)閉流
[self.outputStream close];
// 如果下載回調(diào)不為空
if (self.successBlock) {
// 分發(fā)消息到主線程憾赁,讓主線程來處理
dispatch_async(dispatch_get_main_queue(), ^{
self.successBlock(self.filePath);
});
}
}
// 下載出錯
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// NSLog(@"下載出錯");
// 判斷流是否為空污朽,下載出錯的時候,關(guān)閉流
if (self.outputStream) {
[self.outputStream close];
}
// 判斷異沉迹回調(diào)是否為空
if (self.errorBlock) {
// 不為空回到主線程
dispatch_async(dispatch_get_main_queue(), ^{
self.errorBlock(error);
});
}
}
// NSOutputStream保存數(shù)據(jù)
// data 每次下載的二進(jìn)制文件
- (void)saveData:(NSData *)data {
// 寫數(shù)據(jù)
[self.outputStream write:data.bytes maxLength:data.length];
}
// NSFileHandle保存數(shù)據(jù)
// data 每次下載的二進(jìn)制數(shù)據(jù)
- (void)saveData1:(NSData *)data {
// 保存文件的路徑
NSString *filePath = @"/Users/mazaiting/Desktop/1.zip";
// 根據(jù)路徑獲取NSFileHandle, 它不會自動創(chuàng)建文件蟆肆,而是第0個字節(jié)添加數(shù)據(jù)
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
// 如果fileHandle存在矾睦,則讓fileHandle的offset偏移量指向文件的末尾,進(jìn)行數(shù)據(jù)的添加
// 如果fileHandle不存在炎功,則創(chuàng)建文件
if (fileHandle) {
// fileHandle存在
// 將文件指針指向文件的末尾
[fileHandle seekToEndOfFile];
// 寫數(shù)據(jù)
[fileHandle writeData:data];
// 關(guān)閉文件
[fileHandle closeFile];
} else {
// fileHandle不存在
[data writeToFile:filePath atomically:YES];
}
}
@end