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 = 0;
self.contentLength = 0; }

  利用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
{ }

from :http://www.cnblogs.com/GeekStar/p/4409714.html

【转】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. Java8 使用 stream().map()提取List对象的某一列值及排重

    List对象类(StudentInfo) public class StudentInfo implements Comparable<StudentInfo> { //名称 privat ...

  2. 洛谷P1762 偶数

    P1762 偶数 题目描述 给定一个正整数n,请输出杨辉三角形前n行的偶数个数对1000003取模后的结果. 输入输出格式 输入格式: 一个数 输出格式: 结果 输入输出样例 输入样例#1: 复制 6 ...

  3. mysql之SQL入门与提升(二)

    在mysql之SQL入门与提升(一)我们已经有了些许基础,今天继续深化 先造表 SET NAMES utf8;SET FOREIGN_KEY_CHECKS = 0; -- -------------- ...

  4. JavaScript中内置对象的一些属性及方法

    Javascript对象总结 JS中内置了17个对象,常用的是Array对象.Date对象.正则表达式对象.string对象.Global对象 Array对象中常用方法: Concat():表示把几个 ...

  5. 简述raid0,raid1,raid5,raid10 的工作原理及特点

    RAID 0 支持1块盘到多块盘,容量是所有盘之和 RAID1 只支持2块盘,容量损失一块盘 RAID 5最少三块盘,不管硬盘数量多少,只损失一块容量 RAID 10最少4块盘,必须偶数硬盘,不管硬盘 ...

  6. Excel2010表格里设置每页打印时都有表头

    在打印Excel表格时常常会出现如果存在多页打印时,往往从第二页开始就会出现没有表头的情况,导致到后面都不清楚对应的是哪个数据,查看时也很麻烦,下面就将为大家介绍如何在Excel表格里设置每页打印时都 ...

  7. SGU - 409

    题目链接:https://vjudge.net/contest/239445#problem/H 题目大意:输入n,k,有n*n* n*n的网格,要求每行每列刚好有k个*,每n*n的小方格内也刚好有k ...

  8. (转)Linux系统stat指令用法

    <Linux系统stat指令用法>  原文:https://www.cnblogs.com/linux-super-meng/p/3812695.html stat指令:文件/文件系统的详 ...

  9. Hibernate的事务管理

    Hibernate的事务管理 事务(Transaction)是工作中的基本逻辑单位,可以用于确保数据库能够被正确修改,避免数据只修改了一部分而导致数据不完整,或者在修改时受到用户干扰.作为一名软件设计 ...

  10. <Linux系统isosize指令用法>

    isosize命令:iso9660文件系统大小显示 isosize命令用于显示iso9660文件系统的大小,还文件可以使普通文件,也可以是块设备,如/dev/sr0或者/dev/sda.如果没有相关选 ...