iOS开发网络篇—实现大文件的多线程断点下载

2020-01-18 16:43:03于海丽

说明:本文介绍多线程断点下载。项目中使用了苹果自带的类,实现了同时开启多条线程下载一个较大的文件。因为实现过程较为复杂,所以下面贴出完整的代码。

实现思路:下载开始,创建一个和要下载的文件大小相同的文件(如果要下载的文件为100M,那么就在沙盒中创建一个100M的文件,然后计算每一段的下载量,开启多条线程下载各段的数据,分别写入对应的文件部分)。
ios多线程断点下载,ios,断点续传实现原理,ios开发断点续传
项目中用到的主要类如下:
ios多线程断点下载,ios,断点续传实现原理,ios开发断点续传

完成的实现代码如下:

主控制器中的代码:


#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://www.easck.com/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