ios 文件下载 FTP下载

其实ios的FTP下载非常简单 。其实就是下载数据流。和其他文件下载是一样的。

有两种方法

 

第一种 适合小文件

1 //替换字符
2 
3 NSString* strPath = [builder.Path stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
4 strPath = [strPath stringByReplacingOccurrencesOfString:@"//" withString:@"/"];
5 //下载链接
6 NSString * fileStream = [NSString stringWithFormat : @"%@%@:%@%@%@%@",@"ftp://", builder.USER_NAME, builder.USER_PWD,@"@" ,builder.HOST_NAME,strPath];
7 NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString: fileStream]];
View Code

fileStream:就是下载地址  ftp://USER_PWD:USER_PWD@999.99.999.99/carNet/File/ARCHIVE/2017/10/16/201710161001/1/asd/asdfgChinaSRV02_172.31.132.244_201710161633073.dat

第二种:

 1 NSURL *url=[NSURL URLWithString:fileStream];
 2 //设置请求超时
 3 NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
 4 request.timeoutInterval=5.0;
 5 //发送请求
 6 //使用代理发送异步请求(通常应用于文件下载)'connectionWithRequest:delegate:' is deprecated: first deprecated in iOS 9.0 - U
 7 // NSURLConnection *conn=[NSURLConnection connectionWithRequest:request delegate:self];
 8 NSURLSession *session = [NSURLSession sharedSession];
 9 NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
10 completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
11 {
12 // do something with the data
13 NSData *data1111 =data;
14 NSLog(@"dffffff%@",data1111);
15 }];
16 [dataTask resume];
View Code

在我打log的时候我发现nsdata 里面下载的内容显示不全。一开始我以为是我下载的问题。又研究沙盒,有研究长度。后来我把数据移到nsdata上 直接查看。发现其实已经下载全了。是nslog的显示不全。

 

posted @ 2017-10-25 11:12  懒猫口米  阅读(2738)  评论(0编辑  收藏  举报