博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
多线程--多线程断点下载
阅读量:6965 次
发布时间:2019-06-27

本文共 5570 字,大约阅读时间需要 18 分钟。

---恢复内容开始---

////  HMFileMultiDownloader.m//  08-多线程断点下载////  Created by apple on 14-6-27.//  Copyright (c) 2014年 heima. All rights reserved.//#import "HMFileMultiDownloader.h"#import "HMFileSingleDownloader.h"#define HMMaxDownloadCount 4@interface HMFileMultiDownloader()@property (nonatomic, strong) NSMutableArray *singleDownloaders;@property (nonatomic, assign) long long totalLength;@end@implementation HMFileMultiDownloader- (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 % HMMaxDownloadCount == 0) {            size = self.totalLength / HMMaxDownloadCount;        } else {            size = self.totalLength / HMMaxDownloadCount + 1;        }                // 创建N个下载器        for (int i = 0; i
////  HMFileSingleDownloader.h//  08-多线程断点下载////  Created by apple on 14-6-27.//  Copyright (c) 2014年 heima. All rights reserved.//#import "HMFileDownloader.h"@interface HMFileSingleDownloader : HMFileDownloader/** *  开始的位置 */@property (nonatomic, assign) long long begin;/** *  结束的位置 */@property (nonatomic, assign) long long end; @end////  HMFileSingleDownloader.m//  08-多线程断点下载////  Created by apple on 14-6-27.//  Copyright (c) 2014年 heima. All rights reserved.//#import "HMFileSingleDownloader.h"@interface HMFileSingleDownloader() 
/** * 连接对象 */@property (nonatomic, strong) NSURLConnection *conn;/** * 写数据的文件句柄 */@property (nonatomic, strong) NSFileHandle *writeHandle;/** * 当前已下载数据的长度 */@property (nonatomic, assign) long long currentLength;@end@implementation HMFileSingleDownloader- (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
////  HMFileDownloader.h//  08-多线程断点下载////  Created by apple on 14-6-27.//  Copyright (c) 2014年 heima. All rights reserved.//#import 
@interface HMFileDownloader : 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

 

 

////  HMViewController.m//  08-多线程断点下载////  Created by apple on 14-6-27.//  Copyright (c) 2014年 heima. All rights reserved.//#import "HMViewController.h"#import "HMFileMultiDownloader.h"@interface HMViewController ()@property (nonatomic, strong) HMFileMultiDownloader *fileMultiDownloader;@end@implementation HMViewController-  (HMFileMultiDownloader *)fileMultiDownloader{    if (!_fileMultiDownloader) {        _fileMultiDownloader = [[HMFileMultiDownloader 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

 

---恢复内容结束---

转载于:https://www.cnblogs.com/chen495353880/p/4805168.html

你可能感兴趣的文章
微服务系列(七):将单体应用改造为微服务
查看>>
Centos 配置
查看>>
promise
查看>>
es6学习1: 模拟react Comopnent类的实现
查看>>
js继承问题
查看>>
201621123069 《Java程序设计》第十一周学习总结
查看>>
Java进阶篇(一)——接口、继承与多态
查看>>
linux c 链接详解4-共享库
查看>>
冲刺阶段第七天
查看>>
linux下磁盘分区
查看>>
快速获取表的记录数
查看>>
JavaScript_BOM_window
查看>>
Hadoop:The Definitive Guid 总结 Chapter 7 MapReduce的类型与格式
查看>>
WCF 入门之旅(4): 怎样用客户端调用WCF服务
查看>>
oracle12之 多租户容器数据库架构
查看>>
POJ3061 ZOJ3123 Subsequence【前缀和+二分搜索+尺取法】
查看>>
png库结合zlib库使用出现的一个链接问题的解决
查看>>
Hibernate总结(二)
查看>>
TSP问题
查看>>
ubuntu14.06 Lts开启ssh服务
查看>>