码迷,mamicode.com
首页 > Web开发 > 详细

文件下载 NSURLConnection——小文件下载

时间:2017-04-21 23:06:18      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:pre   component   ted   适合   nec   dfa   finish   writing   句柄   

1.下载小文件,只适合几百k,1、2M的文件

//1.设置网址
//2.加载请求
//3.设置代理
//4.实现代理方法下载文件
 NSURL *url = [NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1492791082124&di=fc642407b4ec19430334653a9b873cff&imgtype=0&src=http%3A%2F%2Fi0.szhomeimg.com%2FUploadFiles%2FBBS%2F2006%2F10%2F24%2F27567833_1950.442.jpg"];  
 NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
代理实现
#pragma mark ---NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"下载失败%@",error);
}
//下载响应
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
//获取文件大小
    _totalSize = response.expectedContentLength;
}
//下载数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    //拼接数据
    [_mudata appendData:data];
    //获取当前进度
    NSLog(@"%f",1.0*_mudata.length/_totalSize);
}
//下载完成
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    
    NSString *fullPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"1.jpg"];
    [_mudata writeToFile:fullPath atomically:YES];
}

  

  缺点:[_mudata appendData:data],会使内存暴涨,而且下载完毕后内存不会下降

2.通过句柄来下载大文件

#pragma mark ----------------------
#pragma mark NSURLConnectionDataDelegate
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"didReceiveResponse");
    
    //1.得到文件的总大小(本次请求的文件数据的总大小)
    self.totalSize = response.expectedContentLength;
    
    //2.写数据到沙盒中
    self.fullPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]stringByAppendingPathComponent:@"123.mp4"];
    
    //3.创建一个空的文件
    [[NSFileManager defaultManager] createFileAtPath:self.fullPath contents:nil attributes:nil];
    
    //4.创建文件句柄(指针)
    self.handle = [NSFileHandle fileHandleForWritingAtPath:self.fullPath];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    //1.移动文件句柄到数据的末尾
    [self.handle seekToEndOfFile];
    
    //2.写数据
    [self.handle writeData:data];
    
    //3.获得进度
    self.currentSize += data.length;
    
    //进度=已经下载/文件的总大小
    NSLog(@"%f",1.0 *  self.currentSize/self.totalSize);
    self.progressView.progress = 1.0 *  self.currentSize/self.totalSize;
    //NSLog(@"%@",self.fullPath);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //1.关闭文件句柄
    [self.handle closeFile];
    self.handle = nil;
    NSLog(@"connectionDidFinishLoading");
    NSLog(@"%@",self.fullPath);
}

 3.通过断点续传来下载大文件

设置请求头,如果文件存在就继续下载

同上
    //2.创建请求对象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSString *range = [NSString stringWithFormat:@"bytes=%zd-",self.currentSize];
    [request setValue:range forHTTPHeaderField:@"Range"];
    //同上
}
#pragma mark ----------------------
#pragma mark NSURLConnectionDataDelegate
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    //1.得到文件的总大小(本次请求的文件数据的总大小)
    self.totalSize = response.expectedContentLength+self.currentSize;
    if (self.currentSize > 0) {
        return;
    }
    //同上
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    //同上
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //同上
}

 缺点:下次运行工程进来就要重新下载  

 

文件下载 NSURLConnection——小文件下载

标签:pre   component   ted   适合   nec   dfa   finish   writing   句柄   

原文地址:http://www.cnblogs.com/TheYouth/p/6746158.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!