总结在前: //----------------------------- //---------MKN下载数据---------- //----------------------------- MKNetworkEngine *MKN = [[MKNetworkEngine alloc] initWithHostName:nil customHeaderFields:nil];//自带了缓存策略;线程池(wifi最大6,wwan最大2);freeze MKNetworkOperation *netOp= [MKN operationWithURLString:downLoadUrl params:nil httpMethod:@"GET"];//继承自NSOperation [netOp addDownloadStream:[NSOutputStream outputStreamToFileAtPath:@"test.pdf" append:NO]];//定义输出流 //----------显示下载进度------------------------------------------------------------------------- MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.view]; [self.view addSubview:HUD]; HUD.tag=1000; HUD.mode = MBProgressHUDModeDeterminate; HUD.labelText = @"正在下载..."; HUD.square = YES; [HUD show:YES]; [netOp onDownloadProgressChanged:^(double progress) {HUD.progress = progress;}];//显示进度条 //-------------------------------------------------------------------------------------------- [netOp onCompletion:^(MKNetworkOperation* completedRequest) {[HUD removeFromSuperview];}//完成 onError:^(NSError* error) {[HUD removeFromSuperview];}]; [MKN enqueueOperation:netOp];//启动线程也可以用[netOp start] //----------------------------- //---------AFN下载数据---------- //----------------------------- NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:pdfurl]];//封装下载对象(get,post数据等) //--------封装POST数据,键值对(GET访问就不需要) NSData *postData = [[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]dataUsingEncoding:NSUTF8StringEncoding];//将键值对(key=value&key=value)封装为NSData [request setHTTPMethod:@"POST"]; [request setValue:[NSString stringWithFormat:@"%d",[postData length]] forHTTPHeaderField:@"Content-Length"];//设置NSData长度 [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:postData];//设置body //----------------------------- AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];////继承自NSOperation operation.outputStream = [NSOutputStream outputStreamToFileAtPath:BillFile(pdfNameExt) append:NO];//定义输出流 //----------显示下载进度------------------------------------------------------------------------- MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.view]; [self.view addSubview:HUD]; HUD.tag=1000; HUD.mode = MBProgressHUDModeDeterminate; HUD.labelText = @"正在下载..."; HUD.square = YES; [HUD show:YES]; [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {//显示进度条 HUD.progress = (totalBytesRead*1.0) / totalBytesExpectedToRead;}]; //-------------------------------------------------------------------------------------------- [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {[HUD removeFromSuperview];} //完成 failure:^(AFHTTPRequestOperation *operation, NSError *error) {[HUD removeFromSuperview];}]; [operation start];//这里可以自定义一个线程池NSOperationQueue,然后设置最大线程数,调用addOperation
//----------------------------- //---------MKN上传数据---------- //----------------------------- MKNetworkEngine *MKN = [[MKNetworkEngine alloc] initWithHostName:nil]; MKNetworkOperation *netOp = [MKN operationWithURLString:uploadUrl params:[NSDictionary dictionaryWithObjectsAndKeys:AppSession.deviceId,@"deviceId",nil] httpMethod:@"POST"]; [netOp addFile:BillFile(newBillName) forKey:@"image"];//添加文件 addFilePath:forFileName [netOp setFreezable:YES];//设置保持 //----------显示上传进度------------------------------------------------------------------------- MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.view]; [self.view addSubview:HUD]; HUD.mode = MBProgressHUDModeDeterminate; HUD.labelText = @"正在上传..."; HUD.square = YES; [HUD show:YES]; [netOp onUploadProgressChanged:^(double progress) {HUD.progress = progress;}];//显示进度条 //-------------------------------------------------------------------------------------------- [netOp onCompletion:^(MKNetworkOperation* completedRequest) {[HUD removeFromSuperview];}//完成 onError:^(NSError* error) {[HUD removeFromSuperview];}]; [MKN enqueueOperation:netOp];//启动线程也可以用[netOp start] //----------------------------- //---------AFN上传数据(未测试)---------- //----------------------------- //NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:pdfurl]];// NSData *imageData = UIImagePNGRepresentation(image);//将图片文件转换为NSData(官方只给了图片上传方法,其它任何类型的文件上传有待测试) AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"192.168.1.106"]]; NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];}]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];////继承自NSOperation //----------显示下载进度------------------------------------------------------------------------- MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.view]; [self.view addSubview:HUD]; HUD.tag=1000; HUD.mode = MBProgressHUDModeDeterminate; HUD.labelText = @"正在下载..."; HUD.square = YES; [HUD show:YES]; [operation setUploadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {//显示进度条 HUD.progress = (totalBytesRead*1.0) / totalBytesExpectedToRead;}]; //-------------------------------------------------------------------------------------------- [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {[HUD removeFromSuperview];} //完成 failure:^(AFHTTPRequestOperation *operation, NSError *error) {[HUD removeFromSuperview];}]; [operation start];//这里可以自定义一个线程池NSOperationQueue,然后设置最大线程数,调用addOperation
//函数作用: 多线程批量下载文件,没下载完成的后缀为.temp // 采用线程池 最大同时运行3个分线程 // 当下载完成时去掉后缀.temp,并删除临时文件
|
|