0

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

- (void)startSend

{ AppDelegate *mainDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];

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];
}

}

它打印,“文件存在”但在下一行“没有内容”

该文件不为空。

4

1 回答 1

2

我只是遇到了同样的问题,答案很简单而且很愚蠢。

创建流后:

self.fileStream = [NSInputStream inputStreamWithFileAtPath:filePath];

你需要调用它:

[self.fileStream open];

现在我知道缺少什么是完全有道理的,但是在我发现的使用 NSInputStream 的示例中我没有看到打开的调用。

于 2013-10-09T00:05:09.200 回答