网络多任务下载,断点下载
来源:互联网 发布:win7 禁止卸载软件 编辑:程序博客网 时间:2024/05/06 16:26
直接上代码:
#pragma mark -下载游戏-(void)loadGameZipWithUrl:(NSString *)gameUrl{ //进度缓存目录 NSString *tempPath = [self getProcessDocument]; //下载游戏的本地地址 NSString * documentpath= [self getGamePahtUrl]; NSString* unzipfilePath =[documentpath stringByAppendingString:@".zip"]; NSFileManager *filemanager = [NSFileManager defaultManager]; //下载地址 NSURL *url = [NSURL URLWithString:gameUrl]; NSURLRequest * request = [NSURLRequest requestWithURL:url]; unsigned long long downloadedBytes =0; //检查已经下载的部分 if ([filemanager fileExistsAtPath:unzipfilePath])//如果存在,说明有缓存文件 { downloadedBytes = [self fileSizeAtPath:unzipfilePath];//计算缓存文件的大小 if(downloadedBytes>0){ NSMutableURLRequest *mutableURLRequest = [request mutableCopy]; NSString *requestRange = [NSString stringWithFormat:@"bytes=%llu-", downloadedBytes]; [mutableURLRequest setValue:requestRange forHTTPHeaderField:@"Range"]; request = mutableURLRequest; NSLog(@"==============断点下载"); } } //初始化队列 NSOperationQueue *queue = [[NSOperationQueue alloc ]init]; //不使用缓存,避免断点续传出现问题 [[NSURLCache sharedURLCache] removeCachedResponseForRequest:request]; AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request]; //设置存储路径 op.outputStream = [NSOutputStream outputStreamToFileAtPath:unzipfilePath append:YES]; __block typeof(op) operation = op; // 下载进度 [op setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) { CGFloat precent =((float)totalBytesRead + downloadedBytes) / (totalBytesExpectedToRead + downloadedBytes); //设置进度 NSLog(@"process ==>%f",precent); NSString * progress = [NSString stringWithFormat:@"%.3f",((float)totalBytesRead + downloadedBytes) / (totalBytesExpectedToRead + downloadedBytes)]; [progress writeToFile:tempPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; }]; //下载成功 [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"下载成功"); NSString *stringUrl = [operation.request.URL absoluteString]; //下载完成,解压游戏,并删除zip包 [self unZipGameWithName:[NSString stringWithFormat:@"%@/%@",documentpath,XXX]]; } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"下载失败"); }]; //开始下载// [op start]; [queue addOperation:op];}#pragma mark-计算缓存文件大小的方法- (unsigned long long)fileSizeAtPath:(NSString *)fileAbsolutePath { signed long long fileSize = 0; NSFileManager *fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:fileAbsolutePath]) { NSError *error = nil; NSDictionary *fileDict = [fileManager attributesOfItemAtPath:fileAbsolutePath error:&error]; if (!error && fileDict) { fileSize = [fileDict fileSize]; } } return fileSize;}
获取缓存进度目录
#pragma mark-获取进度目录-(NSString *)getProcessDocument{ //进度目录 NSString *tempFile =[NSString stringWithFormat:@"%@Temp.txt",GameType_SLYZ]; tempPath=[ NSTemporaryDirectory() stringByAppendingPathComponent:tempFile]; return tempPath;}
#pragma mark - 获取压缩后游戏地址-(NSString*)getGamePahtUrl{ //游戏的目录地址 NSString * documentpath= [self getGamePath]; NSString* gamefilePath = [documentpath stringByAppendingFormat:@"/%@",gType] ; return gamefilePath;}
#pragma mark - 获取压缩包游戏地址-(NSString *)getGamePath{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentpath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; return documentpath;}
1 0
- 网络多任务下载,断点下载
- Android多任务断点下载
- 多线程多任务断点下载
- android 下载文件(多任务,断点下载.....)
- Android 多任务多线程断点下载
- 安卓 多任务 多线程 断点 下载
- android实现多任务断点下载
- 后台多任务多线程断点下载
- 后台多任务多线程断点下载
- ios 多任务断点下载 AFNetWorking
- android实现多任务断点下载
- 网络多线程断点下载
- 网络多线程断点下载
- 网络多线程断点下载
- Android 文件下载(断点、多任务并行下载)
- Android:网络:多线程断点下载
- 网络_断点续传.断点下载
- android 下载文件(支持多任务,支持断点.....)
- 走迷宫
- 几何画板怎样添加操作按钮
- svn冲突文件解决方法
- c++作业3
- shell中的cat和文件分界符(<<EOF)
- 网络多任务下载,断点下载
- JQuery表单验证
- linux下文件夹的创建、复制、剪切、重命名、清空和删除命令
- 在几何画板中怎样添加链接
- An interesting kind of JavaScript memory leak
- 点击uploadify上传按钮没有效果
- 在android中使用SQLite数据库
- linux 命令
- AlertDialog Unable to add window