FTP文传协议的应用
我开发的项目中一直用到都是AFNetworking上传图片的方法,最近老大说要用FTP上传,网上的资料很少,毕竟这种上传方式现在用的不多了,于是花了一天时间学习了FTP文件传输协议。下面是我的个人理解:
首先介绍下FTP是个什么东东?
FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议”。用于Internet上的控制文件的双向传输。同时,它也是一个应用程序(Application)。 基于不同的操作系统有不同的FTP应用程序,而所有这些应用程序都遵守同一种协议以传输文件。在FTP的使用当中,用户经常遇到两个概念:"下载" (Download)和"上传"(Upload)。"下载"文件就是从远程主机(可以理解为另一个服务器)拷贝文件至自己的计算机上;"上传"文件就是将文件从自己的计算机中拷贝至 远程主机上。用Internet语言来说,用户可通过客户机程序向(从)远程主机上传(下载)文件。(摘自百度)注:若使用本机FTP地址,那么本机就相当于一个远程主机。
多说无益,下面我就已上传相册中的图片到FTP:
1,在上传之前肯定是要打开FTP
用FTP的话,高版本的mac os默认关掉了,可以用如下命令打开:
sudo -s launchctl load -w /System/Library/LaunchDaemons/ftp.plist
相应的,要关闭则:
sudo -s launchctl unload -w /System/Library/LaunchDaemons/ftp.plist Mac自带的服务器工具可以查看FTP文件:教程http://jingyan.baidu.com/article/7c6fb42869cd2380642c90af.html (访问的用户名和密码是登录mac系统的用户名和密码)。
2,在马代码之前需要下载一个第三方FTP文件管理SDK https://github.com/ZCasey/GRRequestsManager
3,开始马代码!
思路:a.从相册中取出照片存入指定文件夹(应用中)
b.在ftp中创建文件夹,再将图片上传至此文件夹中
data = dataImage; NSDateFormatter *formate = [[NSDateFormatter alloc] init]; formate.dateFormat = @"yyyy-MM-dd HH:mm:ss"; NSString *documentPath = [NSHomeDirectory() stringByAppendingPathComponent:@"document"]; //打开文件管理器 NSFileManager *fileMange = [NSFileManager defaultManager]; //创建文件夹 [fileMange createDirectoryAtPath:documentPath withIntermediateDirectories:YES attributes:nil error:nil]; //创建文件 NSString *localpath =[NSString stringWithFormat:@"%@/%@.jpg",documentPath,[formate stringFromDate:[NSDate date]]]; NSLog(@"图片路径 %@ :",localpath) [fileMange createFileAtPath:[NSString stringWithFormat:@"%@/%@.jpg",documentPath,[formate stringFromDate:[NSDate date]]] contents:data attributes:nil];
//(需引入头文件#import "GRRequestsManager.h")
GRRequestsManager * requestManager = nil; //设置FTP请求 requestManager = [[GRRequestsManager alloc] initWithHostname:@"ftp://********" user:@"*****" password:@"*********"]; //FTP设置代理 requestManager.delegate = self; //创建目录层上传文件 [requestManager addRequestForCreateDirectoryAtPath:@"ZZTest"]; [requestManager startProcessingRequests]; NSString *ftpPath = [NSString stringWithFormat:@"ZZTest/11111.jpg"] [self.requestManager addRequestForUploadFileAtLocalPath:localpath toRemotePath:ftpPath]; [self.requestManager startProcessingRequests];
GRRequestsManager的代理方法
#pragma mark - GRRequestsManagerDelegate - (void)requestsManager:(id<GRRequestsManagerProtocol>)requestsManager didScheduleRequest:(id<GRRequestProtocol>)request { NSLog(@"1_requestsManager:didScheduleRequest:"); } - (void)requestsManager:(id<GRRequestsManagerProtocol>)requestsManager didCompleteListingRequest:(id<GRRequestProtocol>)request listing:(NSArray *)listing { NSLog(@"2_requestsManager:didCompleteListingRequest:listing: \n%@", listing); } - (void)requestsManager:(id<GRRequestsManagerProtocol>)requestsManager didCompleteCreateDirectoryRequest:(id<GRRequestProtocol>)request { //新建文件夹成功 NSLog(@"3_requestsManager:didCompleteCreateDirectoryRequest:"); } - (void)requestsManager:(id<GRRequestsManagerProtocol>)requestsManager didCompleteDeleteRequest:(id<GRRequestProtocol>)request { NSLog(@"4_requestsManager:didCompleteDeleteRequest:"); } - (void)requestsManager:(id<GRRequestsManagerProtocol>)requestsManager didCompletePercent:(float)percent forRequest:(id<GRRequestProtocol>)request { NSLog(@"5_requestsManager:didCompletePercent:forRequest: %f", percent); } - (void)requestsManager:(id<GRRequestsManagerProtocol>)requestsManager didCompleteUploadRequest:(id<GRDataExchangeRequestProtocol>)request { //文件上传成功 NSLog(@"6_requestsManager:didCompleteUploadRequest:"); } - (void)requestsManager:(id<GRRequestsManagerProtocol>)requestsManager didCompleteDownloadRequest:(id<GRDataExchangeRequestProtocol>)request { NSLog(@"7_requestsManager:didCompleteDownloadRequest:"); } - (void)requestsManager:(id<GRRequestsManagerProtocol>)requestsManager didFailWritingFileAtPath:(NSString *)path forRequest:(id<GRDataExchangeRequestProtocol>)request error:(NSError *)error { NSLog(@"8_requestsManager:didFailWritingFileAtPath:forRequest:error: \n %@", error); } - (void)requestsManager:(id<GRRequestsManagerProtocol>)requestsManager didFailRequest:(id<GRRequestProtocol>)request withError:(NSError *)error { NSLog(@"9_requestsManager:didFailRequest:withError: \n %@", error); }