首页 > 代码库 > 文件下载 NSURLConnection——小文件下载
文件下载 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://www.mamicode.com/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——小文件下载
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。