2011-09-04 118 views
0

我一直在努力,並試圖讓蘋果SimpleFTPSample爲我工作,但我不能。下面是我一直使用的代碼:爲什麼我無法看到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]; 
} 

}

在這個例子中,主機名是FTP服務器的IP地址。我一直得到的問題是,在我的NSLog(@"After url=")網址一直爲空,我無法弄清楚爲什麼它一直被創建爲空。另外,pngPath是我試圖上傳的圖像所在的NSString。有沒有人知道編輯簡單樣本的簡單方法,因爲我已經在這裏工作了幾個星期,並且無法使其工作。

此外,我無法找到他們的API上的任何東西,使其以被動模式發送,有沒有人知道我該如何做這項工作呢?謝謝!

回答

0

操作使用的NSString是計劃協議問題(「FTP://」爲實例),因爲只要您修改的NSString,它將取代由獨特的「/」字符「/」任意序列。

事實上,當您使用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創建ADRESS,最後他們非轉義。

對於被動模式下,使用您的NSStream的 'kCFStreamPropertyFTPUsePassiveMode' 屬性:

[_outputStream setProperty:(_passiveMode ? (id)kCFBooleanTrue : (id)kCFBooleanFalse) forKey:(id)kCFStreamPropertyFTPUsePassiveMode] 
相關問題