大文件的断点下载

Posted on 2016-07-19 23:18  柠檬片  阅读(120)  评论(0)    收藏  举报

请求头部分的设置

//2.创建请求对象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    //2.1 设置下载文件的某一部分
    // 只要设置HTTP请求头的Range属性, 就可以实现从指定位置开始下载
    /*
     表示头500个字节:Range: bytes=0-499
     表示第二个500字节:Range: bytes=500-999
     表示最后500个字节:Range: bytes=-500
     表示500字节以后的范围:Range: bytes=500-
     */
    NSString *range = [NSString stringWithFormat:@"bytes=%zd-",self.currentLength];
    [request setValue:range forHTTPHeaderField:@"Range"];
  1 #import "ViewController.h"
  2 
  3 @interface ViewController ()<NSURLConnectionDataDelegate>
  4 @property (weak, nonatomic) IBOutlet UIImageView *imagView;
  5 @property (weak, nonatomic) IBOutlet UIProgressView *progress;
  6 - (IBAction)startBtnClick:(id)sender;
  7 - (IBAction)suspendBtnClick:(id)sender;
  8 
  9 
 10 
 11 //当前已经下载文件的大小
 12 @property (nonatomic, assign) NSInteger currentLength;
 13 //下载文件的总大小
 14 @property (nonatomic, assign) NSInteger totalLength;
 15 @property (nonatomic, strong)  NSString *fullPath;
 16 //文件句柄
 17 @property (nonatomic, strong) NSFileHandle *handle ;
 18 
 19 @property (nonatomic, strong) NSURLConnection *connect;
 20 @end
 21 
 22 @implementation ViewController
 23 //开始下载
 24 - (IBAction)startBtnClick:(id)sender {
 25     
 26     //1.确定请求路径
 27     NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_01.mp4"];
 28     //2.创建请求对象
 29     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
 30     
 31     /*
 32      表示头500个字节:Range: bytes=0-499
 33      表示第二个500字节:Range: bytes=500-999
 34      表示最后500个字节:Range: bytes=-500
 35      表示500字节以后的范围:Range: bytes=500-
 36      */
 37     NSString *range = [NSString stringWithFormat:@"bytes=%zd-",self.currentLength];
 38     [request setValue:range forHTTPHeaderField:@"Range"];
 39     NSLog(@"%@",range);
 40     
 41     //3.设置代理,发送请求
 42     NSURLConnection *connect =  [NSURLConnection connectionWithRequest:request delegate:self];
 43     self.connect = connect;
 44 }
 45 //暂停
 46 - (IBAction)suspendBtnClick:(id)sender {
 47     
 48     NSLog(@"暂停--------------------%zd-",self.currentLength);
 49     
 50     [self.connect cancel];
 51 }
 52 
 53 
 54 #pragma mark  NSURLConnectionDataDelegate  start
 55 
 56 /*
 57  1.接收到服务器响应的适合调用
 58  */
 59 -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
 60 {
 61     NSLog(@"+++++++++++");
 62     
 63     if (self.currentLength > 0) {
 64         return;
 65     }
 66     NSFileManager *manager = [NSFileManager defaultManager];
 67     
 68     //保存下载的文件到沙河
 69     NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
 70     
 71     //拼接文件全路径
 72     NSString *fullPath = [caches stringByAppendingPathComponent:response.suggestedFilename];
 73     self.fullPath = fullPath;
 74     //创建文件
 75     [manager createFileAtPath:fullPath contents:nil attributes:nil];
 76     
 77     NSLog(@"%@",fullPath);
 78     
 79     
 80     //拿到文件的总大小
 81     self.totalLength = response.expectedContentLength + self.currentLength;
 82     
 83     NSLog(@"%zd",self.totalLength);
 84     
 85     //创建文件句柄,指向数据写入的文件
 86     self.handle = [NSFileHandle fileHandleForWritingAtPath:self.fullPath];
 87     //指向从文件的末尾
 88     [self.handle seekToEndOfFile];
 89 }
 90 
 91 /*
 92  2.接收到服务器返回的数据,会调用多次
 93  */
 94 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
 95 {
 96 
 97     self.currentLength +=data.length;
 98     
 99     //写数据
100     [self.handle writeData:data];
101     
102     NSLog(@"%f",1.0 *self.currentLength / self.totalLength);
103     self.progress.progress = 1.0 *self.currentLength / self.totalLength;
104 }
105 
106 /*
107  3.当请求完成之后调用该方法
108  */
109 -(void)connectionDidFinishLoading:(NSURLConnection *)connection
110 {
111     NSLog(@"connectionDidFinishLoading");
112     [self.handle closeFile];
113     self.handle = nil;
114 }
115 /*
116  4.当请求失败的适合调用该方法,如果失败那么error有值
117  */
118 -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
119 {
120     NSLog(@"didFailWithError");
121     
122 }
123 
124 
125 @end
示例