ios的下载我们可以使用的方法有:NSData、NSURLConnection、NSURLSession还有第三方框架AFNetworking和ASI

  利用NSData方法和NSURLConnection的异步方法仅适合下载较小的文件,因为NSData是把数据一口气下载,下载大文件那么手机的内存会一下子暴涨,大文件下载可以用NSURLConnection代理方法、NSURLSession代理方法、AFNetworking和ASI:

  NSData:

// 在沙盒中得存储路径
NSString *str = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [str stringByAppendingPathComponent:@"test.exe"]; NSLog(@"%@",str); // 下载地址
NSURL *url = [NSURL URLWithString:@"http://68.duote.com.cn/fcengw.exe"]; NSData *data = [NSData dataWithContentsOfURL:url]; // 数据写入到沙盒
[data writeToFile:path atomically:YES];

  NSURLConnection异步方法:

// 在沙盒中得存储路径
NSString *str = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [str stringByAppendingPathComponent:@"test.exe"]; NSLog(@"%@",str); // 下载地址
NSURL *url = [NSURL URLWithString:@"http://68.duote.com.cn/fcengw.exe"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; // 异步方法
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { // 数据写入沙盒
[data writeToFile:path atomically:YES];
}];

  利用NSURLConnection的代理方法来实现下载(NSURLConnectionDataDelegate):

  这其中使用了响应头expectedContentLength来获取文件的总大小,利用了请求头的键“Range”来设置暂停后下一次开始时的位置,利用了NSFileManager和NSFileHandle来设置分段存储下载的数据:

#pragma mark - 属性
// 文件句柄对象
@property (nonatomic,strong) NSFileHandle *fileHandle; // 文件总长度
@property (nonatomic,assign) long long contentLength; // 当前文件长度
@property (nonatomic,assign) long long currentLen; @property (nonatomic,strong) NSURLConnection *conn; #pragma mark - 点击事件
// 按钮点击事件
- (IBAction)startAndStop { // 设置按钮选中与否
_btn.selected = !_btn.selected; // 实现断点续传
if (_btn.selected){ // 按钮选中 // 下载地址
NSString *str = @"http://51.duote.org/haozip_duote.exe"; NSURL *url = [NSURL URLWithString:str]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 设置请求头数据
NSString *range = [NSString stringWithFormat:@"bytes=%lld-",self.currentLen];
[request setValue:range forHTTPHeaderField:@"Range"]; self.conn = [NSURLConnection connectionWithRequest:request delegate:self]; }else{ // 取消下载(暂停)
[self.conn cancel];
self.conn = nil;
}
} #pragma mark - NSURLConnectionDataDelegate代理
// 出错
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{ NSLog(@"出错 - %@",error.localizedDescription);
} // 接收到响应
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
if (self.currentLen) return; NSString *cacheStr = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [cacheStr stringByAppendingPathComponent:@"test.exe"]; // 创建一个空的文件
NSFileManager *mgr = [NSFileManager defaultManager];
[mgr createFileAtPath:path contents:nil attributes:nil];

  // 句柄对象实例化
self.fileHandle = [NSFileHandle fileHandleForWritingAtPath:path]; // 文件总大小(响应头取得)
NSHTTPURLResponse *resp = (NSHTTPURLResponse *)response;
self.contentLength = resp.expectedContentLength; } // 接收数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// 找到文件的末尾
[self.fileHandle seekToEndOfFile]; // 在末尾写入数据
[self.fileHandle writeData:data]; // 当前已下载长度
self.currentLen += data.length; // 下载百分比:当前已下载大小 / 文件总大小
self.progressView.progress = (double)self.currentLen / self.contentLength; NSLog(@"%f",self.progressView.progress); } // 接收完成
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"接收完成"); // 关闭
[self.fileHandle closeFile];
self.fileHandle = nil; self.currentLen = ;
self.contentLength = ; }

  利用NSURLSession的代理方法:

  NSURLSession是ios7后才有的方法,苹果官方旨在替代NSURLConnection

  NSURLSessionDataTask:解决一般的GET\POST的请求方法

  NSURLSessionDownloadTask:解决文件下载

  NSURLSessionUploadTask:文件上传

  A、NSURLSessionDataTask的操作:

// 下载地址
NSURL *url = [NSURL URLWithString:@"http://68.duote.com.cn/fcengw.exe"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { // 这里进行json或XML解析
}]; // 3.开始任务
[task resume];

  B、NSURLSessionDownloadTask:不能看到下载进度

// session对象
NSURLSession *session = [NSURLSession sharedSession]; // 下载地址
NSString *str = @"http://51.duote.org/haozip_duote.exe"; // 创建一个下载task
NSURL *url = [NSURL URLWithString:str];
NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) { ////// 因为任务下载是存在沙盒的temp文件中,并且下载完成后会被立即清除,所以这里将下载的文件剪切到caches中 // location : 临时文件的路径(下载好的文件) NSString *cacheStr = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
// response.suggestedFilename : 建议使用的文件名,一般跟服务器端的文件名一致
NSString *path = [cacheStr stringByAppendingPathComponent:response.suggestedFilename]; // 将临时文件剪切或者复制Caches文件夹
NSFileManager *mgr = [NSFileManager defaultManager]; // AtPath : 剪切前的文件路径
// ToPath : 剪切后的文件路径
[mgr moveItemAtPath:location.path toPath:path error:nil];
}]; // 开始任务
[task resume];

  C:NSURLSessionDownloadDelegate的实现:

NSURLSessionConfiguration *cfg = [NSURLSessionConfiguration defaultSessionConfiguration];

// session对象
NSURLSession *session = [NSURLSession sessionWithConfiguration:cfg delegate:self delegateQueue:[NSOperationQueue mainQueue]]; // 下载task
NSURL *url = [NSURL URLWithString:@"http://51.duote.org/haozip_duote.exe"];
NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url]; // 这里不要用block,如果用了block,那么方法会优先调用block,而不是代理方法
// 开始任务
[task resume]; #pragma mark - NSURLSessionDownloadDelegate代理
// 下载完毕后调用
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{ NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
// response.suggestedFilename : 建议使用的文件名,一般跟服务器端的文件名一致 NSString *file = [caches stringByAppendingPathComponent:downloadTask.response.suggestedFilename]; // 将临时文件剪切或者复制Caches文件夹
NSFileManager *mgr = [NSFileManager defaultManager]; // AtPath : 剪切前的文件路径
// ToPath : 剪切后的文件路径
[mgr moveItemAtPath:location.path toPath:file error:nil];
} // 恢复下载时调用 - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes
{ } // 每当下载一部分就会调用,可能会被调用多次
// bytesWritten 这次下载了多少
// totalBytesWritten 当前总共下载了多少到沙盒
// totalBytesExpectedToWrite 文件的总大小
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
double progress = (double)totalBytesWritten / totalBytesExpectedToWrite;
NSLog(@"下载进度---%f", progress);
}

  D、NSURLSession实现断点续传

// .m文件
@property (nonatomic, strong) NSURLSessionDownloadTask *task;
@property (nonatomic, strong) NSData *resumeData;
@property (nonatomic, strong) NSURLSession *session; - (NSURLSession *)session
{
if (!_session) {
// 获得session
NSURLSessionConfiguration *cfg = [NSURLSessionConfiguration defaultSessionConfiguration];
self.session = [NSURLSession sessionWithConfiguration:cfg delegate:self delegateQueue:[NSOperationQueue mainQueue]];
}
return _session;
} // 按钮点击事件
- (IBAction)download:(UIButton *)sender {
// 按钮状态取反
sender.selected = !sender.isSelected; if (self.task == nil) { // 开始(继续)下载
if (self.resumeData) { // 恢复
[self resume];
} else { // 开始
[self start];
}
} else { // 暂停
[self pause];
}
} // 开始下载
- (void)start
{
// 创建一个下载任务
NSURL *url = [NSURL URLWithString:@"http://xxx.com/mmg/cc.exe"];
self.task = [self.session downloadTaskWithURL:url]; // 开始任务
[self.task resume];
} // 恢复下载
- (void)resume
{
// 传入上次暂停下载返回的数据,就可以恢复下载
self.task = [self.session downloadTaskWithResumeData:self.resumeData]; // 开始任务
[self.task resume]; // 清空
self.resumeData = nil;
} // 暂停下载
- (void)pause
{
__weak typeof(self) vc = self;
[self.task cancelByProducingResumeData:^(NSData *resumeData) {
// resumeData : 包含了继续下载的开始位置\下载的url
vc.resumeData = resumeData;
vc.task = nil;
}];
} #pragma mark - NSURLSessionDownloadDelegate 下载代理方法
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location
{
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
// response.suggestedFilename : 建议使用的文件名,一般跟服务器端的文件名一致
NSString *file = [caches stringByAppendingPathComponent:downloadTask.response.suggestedFilename]; // 将临时文件剪切或者复制Caches文件夹
NSFileManager *mgr = [NSFileManager defaultManager]; // AtPath : 剪切前的文件路径
// ToPath : 剪切后的文件路径
[mgr moveItemAtPath:location.path toPath:file error:nil];
} - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didWriteData:(int64_t)bytesWritten
totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
NSLog(@"获得下载进度--%@", [NSThread currentThread]);
// 获得下载进度
self.progressView.progress = (double)totalBytesWritten / totalBytesExpectedToWrite;
} - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didResumeAtOffset:(int64_t)fileOffset
expectedTotalBytes:(int64_t)expectedTotalBytes
{ }

iOS 文件下载及断点续传的更多相关文章

  1. 【转】iOS 文件下载及断点续传

    ios的下载我们可以使用的方法有:NSData.NSURLConnection.NSURLSession还有第三方框架AFNetworking和ASI 利用NSData方法和NSURLConnecti ...

  2. iOS开发之网络编程--4、NSURLSessionDataTask实现文件下载(离线断点续传下载) <进度值显示优化>

    前言:根据前篇<iOS开发之网络编程--2.NSURLSessionDownloadTask文件下载>或者<iOS开发之网络编程--3.NSURLSessionDataTask实现文 ...

  3. 【SFTP】使用Jsch实现Sftp文件下载-支持断点续传和进程监控

    参考上篇文章: <[SFTP]使用Jsch实现Sftp文件下载-支持断点续传和进程监控>:http://www.cnblogs.com/ssslinppp/p/6248763.html  ...

  4. iOS开发之网络编程--3、NSURLSessionDataTask实现文件下载(离线断点续传下载)

    前言:使用NSURLSessionDownloadTask满足不这个需要离线断点续传的下载需求,所以这里就需要使用NSURLSessionDataTask的代理方法来处理下载大文件,并且实现离线断点续 ...

  5. iOS 文件下载

    iOS 视频音乐类等应用会用到“文件下载”.文件下载在iOS中的实现如下: 1.小文件下载 @interface ViewController () <NSURLConnectionDataDe ...

  6. 李洪强iOS开发之断点续传1

    未完待续.. // //  ViewController.m //  A18 - duo wen jian shang chuan // //  Created by 李洪强 on 16/6/29. ...

  7. C# 文件下载之断点续传

    注意,本文所说的断点续传特指 HTTP 协议中的断点续传.本文主要聊聊思路和关键代码,更多细节请参考本文附带的 demo. 工作原理 HTTP 协议中定义了一些请求/响应头,通过组合使用这些头信息.我 ...

  8. Winform文件下载之断点续传

    在本系列的前两篇文章中,分别向大家介绍了用于完成下载任务的 WebClinet 和 WinINet 的基本用法和一些实用技巧. 今天来为大家讲述下载过程中最常遇到的断点续传问题. 首先明确一点,本文所 ...

  9. iOS 文件下载和打开

    最近的项目要用到一个在线报告的下载,于是完成后自己在理一下思路,大体的实现了我要得需求. 话不多说,直接上代码 首先,取到网络文件的链接,进行判段是否需求再次下载还是直接打开 #pragma mark ...

随机推荐

  1. jquery解析XML在IE7下不兼容的问题

    jquery在解析XML内容的时候在IE7下无法显示,是因为数据格式的问题,解决办法如下: $.ajax({        type:"POST",        url:&quo ...

  2. 我的视频网站开通,第一个 ArcGIS文本文件,excel文件生成点操作发布,希望大家支持

    网站地址:http://i.youku.com/gisoracle第一个学习视屏:ArcGIS文本文件,excel文件生成点操作http://v.youku.com/v_show/id_XNzM3Nz ...

  3. 任务十六:零基础JavaScript编码(四)

    任务目的 在上一任务基础上继续JavaScript的体验 深入学习JavaScript的事件机制及DOM操作 学习事件代理机制 学习简单的表单验证功能 学习外部加载JavaScript文件 任务描述 ...

  4. 实验一:使用符号执行工具klee对软件进行破解(来自于klee官网)

    原文地址:https://gitlab.com/Manouchehri/Matryoshka-Stage-2/blob/master/stage2.md 实验用代码下载地址:https://gitla ...

  5. canvas.addEventListener()

    对 canvas 元素的事件监听用addEventListener()实现, 但是有一点缺陷是:canvas 不支持键盘事件,为了解决这个问题,可以采用以下两种方案: 方案一: key event - ...

  6. SQL Server ->> 尝试优化ETL中优化Merge性能

    这几天突发想到在ETL中Merge性能的问题.思路的出发点是Merge到目标表需要扫描的数据太多,而现实情况下,假设应该是只有一小部分会被更新,而且这部分数据也应该是比较新的数据,比方说对于想Fact ...

  7. Stage划分和Task最佳位置算法源码彻底解密

    本课主题 Job Stage 划分算法解密 Task 最佳位置算法实现解密 引言 作业调度的划分算法以及 Task 的最佳计算位置的算法,因为 Stage 的划分是DAGScheduler 工作的核心 ...

  8. July 24th 2017 Week 30th Monday

    The only limit to our realization of tomorrow will be our doubts of today. 实现明天理想的唯一障碍就是今天的疑虑. When ...

  9. mongodb---js脚本操作速记

    之前写一些mongodb的同步或操作程序,往往使用perl,甚至c实现,这样程序很繁琐,而且逻辑不好控制,甚至一些功能和命令什么的,在这些语言的mongo驱动中就没有实现.后来发现mongodb 的s ...

  10. Vue.js经典开源项目汇总

    Vue.js经典开源项目汇总 原文链接:http://www.cnblogs.com/huyong/p/6517949.html Vue是什么? Vue.js(读音 /vjuː/, 类似于 view) ...