![](/img/trans.png)
[英]Can't read response from Java server in iOS client using NSInputStream
[英]Can't read custom file from NSInputStream
我正在嘗試通過NSInputStream從我的文檔目錄中讀取自定義文件,以將其上傳到FTP服務器。 我使用的代碼與Apple提供的SimpleFTPSample中演示的代碼完全相同。 只要文件為空,它似乎可以正常工作,但是一旦包含數據,它就會失敗。
這是我的代碼。 這是我創建輸入流的方式,我嘗試了兩種方式:
//Approach one: Init with path
self.fileStream = [NSInputStream inputStreamWithFileAtPath:filePath];
//Approach two: Init with data
NSData* fileData = [NSData dataWithContentsOfFile:filePath];
self.fileStream = [NSInputStream inputStreamWithData:fileData];
如果我使用數據初始化流,則調用流上的read
(下面的代碼如下)時,會得到EXC_BAD_ACCESS (code=1, address=0x0)
),如果我使用的路徑將其跳轉到File read error
。
filePath
為@"/var/mobile/Applications/94292A2A-37FC-4D8E-BDA6-A26963932AE6/Documents/1395576645.cpn"
, NSData
正確返回並具有806個字節。
那是stream:handleEvent:
委托方法的一部分:
if (self.bufferOffset == self.bufferLimit) {
NSInteger bytesRead;
bytesRead = [self.fileStream read: self.buffer maxLength: kSendBufferSize];
if (bytesRead == -1) {
if (kPrintsToConsole) NSLog(@"File read error");
} else if (bytesRead == 0) {
[self stopSending];
} else {
self.bufferOffset = 0;
self.bufferLimit = bytesRead;
}
}
我有點卡住了。 希望你們能幫助我。 運行iOS 7.1和Xcode 5.1
您需要在讀取之前調用[self.fileStream open]。 對於文件和數據方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.