2011-09-04 2 views
0

나는 Apples 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=")에서 url이 계속 null이되고 null로 생성되는 이유를 알 수 없다는 것입니다. 또한, pngPath는 내가 업로드하려고하는 이미지가있는 NSString입니다. 아무도 간단한 샘플을 편집하는 간단한 방법을 알고 있습니까? 왜냐하면 저는 2 주 동안이 작업을 해왔으므로 제대로 작동하지 않습니다.

또한 API에서 수동 모드로 전송할 수있는 항목을 찾을 수 없으며 누구나 내가 그 작업을 수행 할 수있는 방법을 알고 있습니까? 감사!

답변

0

NSString을 수정하면 모든 "/"문자 시퀀스가 ​​고유 한 "/"로 바뀌므로 NSString을 사용한 작업은 스키마 프로토콜 (예 : "ftp : //")에서 문제가됩니다.

사실 NSString 클래스의 "pathComponent"또는 "path"함수를 사용하면 NSString이 아닌 NSPathStore2 Class 객체를 반환 할 때 "/"에 문제가 발생합니다. 그것은 "/"로 펑키 한 것을 만드는이 클래스 (NSPathStore2)입니다.

그런 다음이를 사용하여 NSURL을 만들려고하면 주소의 시작이 "ftp : /myhost/mypath/myfile.ext"가되므로 실패합니다. "ftp : /"다음에 하나의 "/"가 누락되었습니다.

가이 문제를 방지하려면, 여기에 내가 빨리 무슨 짓을했는지 :
NSString *myPath = [[@"ftp:%2F%2FmyFtpDomain/myPath" 
           stringByAppendingPathComponent:@"myFilename.ext"] 
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

내가 탈출 "/"의 이스케이프 코드 % 2F로 ADRESS의 창조에서, 그리고 마지막을 이스케이프.

[_outputStream setProperty:(_passiveMode ? (id)kCFBooleanTrue : (id)kCFBooleanFalse) forKey:(id)kCFStreamPropertyFTPUsePassiveMode] 
: 수동 모드의

, 당신의 NSStream의 'kCFStreamPropertyFTPUsePassiveMode'속성을 사용
관련 문제