Ftp 将.txt文件读入NSInputstream将返回filestream没有可用字节

Ftp 将.txt文件读入NSInputstream将返回filestream没有可用字节,ftp,objective-c-category,nsinputstream,Ftp,Objective C Category,Nsinputstream,我使用NSInputstream读取文件。读取后,NSInputstream内容为空。我使用了代码(用于将.txt文件传输到ftp服务器) { AppDelegate*mainDelegate=(AppDelegate*)[[UIApplication sharedApplication]委托] BOOL success; NSURL * url; NSUserDefaults *defaults = [NSUserDefa

我使用NSInputstream读取文件。读取后,NSInputstream内容为空。我使用了代码(用于将.txt文件传输到ftp服务器)

{ AppDelegate*mainDelegate=(AppDelegate*)[[UIApplication sharedApplication]委托]

BOOL                    success;
NSURL *                 url;

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSString *testsessionid=[defaults stringForKey:@"testsessionid"];
NSString *writeFileName=[NSString stringWithFormat:@"%@%@.txt",testsessionid,mainDelegate.studentID];
NSLog(@"Write file name %@",writeFileName);
NSArray *searchPaths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentFolderPath = [searchPaths objectAtIndex: 0];

NSString *filePath= [documentFolderPath stringByAppendingPathComponent: writeFileName];
NSLog(@"Write folder name %@",filePath);


filePath=@"/Users/sree/Desktop/ARATHY/BCLSTestApp/BCLSTest/Question2.txt";

assert(filePath != nil);
assert([[NSFileManager defaultManager] fileExistsAtPath:filePath]);
assert( [filePath.pathExtension isEqual:@"txt"]  );

assert(self.networkStream == nil);      // don't tap send twice in a row!
assert(self.fileStream == nil);
// First get and check the URL.

url = [NSURL URLWithString:@"ftp://edugame@www.edugame.co/bclstest/243"];
success = (url != nil);

if (success) {
    // 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.

    url = CFBridgingRelease(
                            CFURLCreateCopyAppendingPathComponent(NULL, ( CFURLRef) url, (CFStringRef) [filePath lastPathComponent], false)
                            );
    success = (url != nil);
}

// 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:filePath];

 //   self.fileStream=[[NSInputStream alloc] initWithFileAtPath:filePath];
    if(self.fileStream==nil)

        NSLog(@"FILE DOESN'T EXIST");

    else
        NSLog(@"FILE EXISTS");

    assert(self.fileStream != nil);
    BOOL hasbyte=[self.fileStream hasBytesAvailable];

    if (hasbyte==YES)

        NSLog(@"Has contents");

    else
        NSLog(@"no contents");

    [self.fileStream open];
  //  NSLog(@"SIZE OF STREAM IS >> %d",fi);
    // Open a CFFTPStream for the URL.

    self.networkStream = CFBridgingRelease(
                                           CFWriteStreamCreateWithFTPURL(NULL, ( CFURLRef) url)
                                           );
    assert(self.networkStream != nil);

        success = [self.networkStream setProperty:@"edugame" forKey:(id)kCFStreamPropertyFTPUserName];
        assert(success);
        success = [self.networkStream setProperty:@"edu1@Game" forKey:(id)kCFStreamPropertyFTPPassword];
        assert(success);


    self.networkStream.delegate = self;
    [self.networkStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [self.networkStream open];

    // Tell the UI we're sending.

    //[self sendDidStart];
}
}

它打印“文件存在” 但在下一行“无内容”


文件不是空的。

我也有同样的问题,答案很简单,也很愚蠢

创建流后:

self.fileStream = [NSInputStream inputStreamWithFileAtPath:filePath];
您需要在其上调用open:

[self.fileStream open];
现在我知道缺少什么了,这完全有道理,但在我找到的使用NSInputStream的示例中,我没有看到打开的调用

[self.fileStream open];