我一直在尝试让Apples SimpleFTPSample为我工作,但我不能。这是我一直在使用的代码:
BOOL success;
CFWriteStreamRef ftpStream;
NSLog(@"Hello");
// assert(self.networkStream == nil); // don't tap send twice in a row!
// assert(self.fileStream == nil); // ditto
// First get and check the URL.
url = [NSURL URLWithString:@hostname];
success = (url != nil);
NSLog(@"After first success");
if (success) {
NSLog(@"In the success if");
// Add the last part of the file name to the end of the URL to form the final
// URL that we're going to put to.
if(fileName) NSLog(@"Filename.");
if(url) NSLog(@"url");
url = [NSMakeCollectable(
CFURLCreateCopyAppendingPathComponent(NULL, (CFURLRef) url,
//(CFStringRef) [fileName lastPathComponent],
(CFStringRef)pngPath,
false)
) autorelease];
[url retain];
NSLog(@"After url=");
success = (url != nil);
assert(success);
NSLog(@"End of success if");
}
// If the URL is bogus, let the user know. Otherwise kick off the connection.
if ( ! success) {
NSLog(@"Invalid URL");
} else {
// Open a stream for the file we're going to send. We do not open this stream;
// NSURLConnection will do it for us.
self.fileStream = [NSInputStream inputStreamWithFileAtPath:pngPath];
assert(self.fileStream != nil);
[self.fileStream open];
// Open a CFFTPStream for the URL.
ftpStream = CFWriteStreamCreateWithFTPURL(NULL, (CFURLRef) url);
if(!ftpStream) NSLog(@"ftpStream is null");
assert(ftpStream != NULL);
self.networkStream = (NSOutputStream *) ftpStream;
#pragma unused (success) //Adding this to appease the static analyzer.
success = [self.networkStream setProperty:@"JEFF" forKey:(id)kCFStreamPropertyFTPUserName];
assert(success);
success = [self.networkStream setProperty:@"jeffrack" forKey:(id)kCFStreamPropertyFTPPassword];
assert(success);
self.networkStream.delegate = self;
[self.networkStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.networkStream open];
// Have to release ftpStream to balance out the create. self.networkStream
// has retained this for our persistent use.
CFRelease(ftpStream);
// Tell the UI we're sending.
[self _sendDidStart];
}
}
在此示例中,hostname是FTP服务器的IP地址。我一直遇到的问题是,在我的NSLog(@"After url=")
url保持为null,我无法弄清楚为什么它一直被创建为null。此外,pngPath是我正在尝试上传的图像所在的NSString。有没有人知道一种编辑简单样本的简单方法,因为我已经在这里工作了几个星期而且无法让它工作。
另外,我在他们的API上找不到任何东西让它以被动模式发送,有人知道我怎么能做这项工作吗?谢谢!
答案 0 :(得分:0)
使用NSString的操作对于方案协议(例如“ftp://”)是有问题的,因为只要修改NSString,它就会用唯一的“/".//替换任何”/“字符序列。 p>
实际上,当您使用NSString类中的任何“pathComponent”或“path”函数时,使用“/”会出现问题,它不会返回NSString而是返回NSPathStore2类对象。就是这个类(NSPathStore2)用“/".
制作时髦的东西然后,当您尝试使用它来创建NSURL时,它将失败,因为地址的开头变为“ftp:/myhost/mypath/myfile.ext”。 “ftp:/”之后缺少一个“/”。
为了避免这个问题,这就是我快速做的事情:
NSString *myPath = [[@"ftp:%2F%2FmyFtpDomain/myPath"
stringByAppendingPathComponent:@"myFilename.ext"]
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
我通过其转义的代码%2F在地址创建时逃脱了“/”,最后没有使用它们。
对于被动模式,使用NSStream的'kCFStreamPropertyFTPUsePassiveMode'属性:
[_outputStream setProperty:( _passiveMode ? (id)kCFBooleanTrue : (id)kCFBooleanFalse ) forKey:(id)kCFStreamPropertyFTPUsePassiveMode]