一、问题描述:1.下载失败报错:
文件写入失败Error Domain=NSCocoaErrorDomain Code=512 "未能将文件“jquery_1_10_2_min.js”存储到文件夹“Q20180104153006399”中。"
原因及解决方法:文件夹下均为文件,不包含子文件夹,GoldRaccoon第三方不会创建存储的本地文件夹,所以需要自己提前手动创建好下载的本地文件夹
NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *zhenggedizhi1 = [documentsDirectoryPath stringByAppendingString:[NSString stringWithFormat:@"/%@/Q20180104153006399",@"OralQuesBank"]]; [fileManager createDirectoryAtPath:zhenggedizhi1 withIntermediateDirectories:YES attributes:nil error:nil];
二、补充:GoldRaccoon基本使用方法
第三方GoldRaccoon使用方法主要参考网址:https://blog.csdn.net/wobushizhaoqian/article/details/72845898?locationNum=10&fps=1
代码:
/** 下载FTP资源 */ #import "GRRequestsManager.h" #import "GRListingRequest.h" #import "FCFileManager.h" <GRRequestsManagerDelegate> @property (nonatomic, strong) GRRequestsManager *requestsManager;
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor orangeColor]; UIButton *downBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [downBtn setTitle:@"下载" forState:UIControlStateNormal]; downBtn.backgroundColor = [UIColor cyanColor]; [downBtn addTarget:self action:@selector(startDownClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:downBtn]; [downBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(self.view); make.size.mas_equalTo(CGSizeMake(50, 40)); }]; } - (void)startDownClick{ //列表localuser下的文件夹目录,这里做的只是列表目录下面有什么 [self.requestsManager addRequestForListDirectoryAtPath:@"OralQuesBank/Q20180104153006399"]; [self.requestsManager startProcessingRequests]; } #pragma mark *** GRRequestsManagerDelegate *** //代理方法,每次执行列表方法都会走这个方法 - (void)requestsManager:(id<GRRequestsManagerProtocol>)requestsManager didCompleteListingRequest:(id<GRRequestProtocol>)request listing:(NSArray *)listing{ //沙盒目录 NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; GRListingRequest *req = (GRListingRequest *)request; //远程文件夹列表 NSLog(@"%@",listing); //打印创建的目录 NSLog(@"%@",documentsDirectoryPath); NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *zhenggedizhi1 = [documentsDirectoryPath stringByAppendingString:[NSString stringWithFormat:@"/%@/Q20180104153006399",@"OralQuesBank"]]; [fileManager createDirectoryAtPath:zhenggedizhi1 withIntermediateDirectories:YES attributes:nil error:nil]; //文件夹列表枚举 [listing enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([[obj pathExtension] isEqualToString:@""]) { //整个地址,在沙盒中创建和远程文件一样的文件夹目录 NSString *zhenggedizhi = [documentsDirectoryPath stringByAppendingString:[NSString stringWithFormat:@"%@/%@",req.path,obj]]; //如果沙盒中的这个文件夹不存在就创建 if (![fileManager fileExistsAtPath:zhenggedizhi]) { [fileManager createDirectoryAtPath:zhenggedizhi withIntermediateDirectories:YES attributes:nil error:nil]; } //然后接着递归列举文件夹下的子文件夹下的目录 [self.requestsManager addRequestForListDirectoryAtPath:[NSString stringWithFormat:@"%@%@",req.path,obj]]; [self.requestsManager startProcessingRequests]; }else{ //如果目录下的不是文件夹,则下载这个文件到沙盒的指定目录下 NSString *zhenggedizhi = [documentsDirectoryPath stringByAppendingString:[NSString stringWithFormat:@"%@/%@",req.path,obj]]; //如果沙盒中的这个文件夹不存在就创建 if (![fileManager fileExistsAtPath:zhenggedizhi]) { [fileManager createDirectoryAtPath:zhenggedizhi withIntermediateDirectories:YES attributes:nil error:nil]; } [self.requestsManager addRequestForDownloadFileAtRemotePath:[NSString stringWithFormat:@"%@/%@",req.path,obj] toLocalPath:[documentsDirectoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@",req.path,obj]]]; [self.requestsManager startProcessingRequests]; } }]; } - (void)requestsManager:(id<GRRequestsManagerProtocol>)requestsManager didFailRequest:(id<GRRequestProtocol>)request withError:(NSError *)error { NSLog(@"请求失败requestsManager:didFailRequest:withError: %@", error); // NSString *errorStr = [NSString stringWithFormat:@"%@",error]; // [self downLoadFail];//可添加处理下载失败后自己的删除文件夹路径方法 } - (void)requestsManager:(id<GRRequestsManagerProtocol>)requestsManager didFailWritingFileAtPath:(NSString *)path forRequest:(id<GRDataExchangeRequestProtocol>)request error:(NSError *)error{ LSLog(@"文件写入失败%@",error); } - (void)requestsManagerDidCompleteQueue:(id<GRRequestsManagerProtocol>)requestsManager{ LSLog(@"整个文件夹下载完成+++++++++++++++++"); } #pragma mark - layzy - (GRRequestsManager *)requestsManager{ if (!_requestsManager) { _requestsManager = [[GRRequestsManager alloc] initWithHostname:@"ftp://11.11.111.11:12" user:@"Administrator" password:@"这是ftp密码"]; // 设置代理 _requestsManager.delegate = self; } return _requestsManager; }