@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){










