2011-07-16 63 views
4

我想FTP文件到遠程機器上。下面是我的代碼: -0 kb文件創建一次FTP在java中完成

FTPClient ftpClient = new FTPClient(); 
ftpClient.connect("home.abc.com"); 
ftpClient.login("remote", "guesst12"); 
int replyCode = ftpClient.getReplyCode(); 
ftpClient.changeWorkingDirectory("share")) 
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); 
InputStream input = new FileInputStream(new File("H:/testFile.txt")); 
OutputStream out = ftpClient.storeFileStream("testFile.txt"); 
Util.copyStream(input, out); 
out.close(); 
input.close(); 
ftpClient.completePendingCommand() 
ftpClient.logout(); 
ftpClient.disconnect(); 

當我執行這段代碼,該代碼是沒有任何問題的執行,但在遠程機器上,當我檢查的文件,正在創建的文件,但沒有內容(OKB)文件。我在代碼中丟失了什麼嗎?

[更新]: 我試圖與用於存儲文件下面的代碼: -

if(ftpClient.storeFile("testCopy.txt", input)) { 
    System.out.println("File Stored Successfully"); 
} 
System.out.println(ftpClient.getReplyString()); 

現在答覆代碼我收到的是: - 451 Failure writing to local file.是什麼意思。

謝謝

+0

+1來補償downvote。誰downvoted:關心解釋? –

+0

這似乎是http://stackoverflow.com/questions/6717684/getting-an-error-while-ftp-file-using-ftpclient的延續。 –

+0

另外,它看起來很像http://stackoverflow.com/questions/6707682/ftp-a-file-to-server-but-it-ftp-with-zero-byte-size。 –

回答

2

一遍又一遍地看着它,我不斷想出不同的東西。

您確定InputStream在複製流之前正在讀取文件嗎?因爲我不確定FileInputStream在啓動時讀取文件。

+1

根據這個問題的接受答案,當調用close()時,顯然這個流總是會被刷新:http://stackoverflow.com/questions/2732260/ in-java-when-i-call-outputstream-close-i-always-need-to-call-outputstream-flus –

+0

@Josh:我也試過,但結果是一樣的。 –

+0

嗯,我不確定,但我知道flush();過去對我來說存在一些問題。任何我會用一個新想法編輯我的文章。 – Josh

0

我懷疑問題出在你沒有提供的代碼Util.copyStream。我強烈建議您使用IOutils from Apache Commons IO複製流。

+0

我試過了,但問題仍然是一樣的。 –

+0

你確定沒有拋出異常嗎?我建議你在複製方法中使用調試器,並查看數據是否實際上正在傳輸。 –

0

一個試圖複製在FTP 文件,特別是如果你看到在服務器端創建0大小的文件時遇到了FTP錯誤451的原因, 可能是由於磁盤上沒有空