文件下载 NSURLConnection——小文件下载
Posted 越自律,越自由!!!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件下载 NSURLConnection——小文件下载相关的知识,希望对你有一定的参考价值。
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——小文件下载的主要内容,如果未能解决你的问题,请参考以下文章
文件下载(NSURLConnection/NSURLSession)
NSURLConnection 有效,但 AFNetworking 无效(在特定 URL 上)
IOS:多个 NSUrlConnection 以及多个 UIProgressBar