2014-01-21 123 views
0

我在我的ftp服務器上有文本文件。我試圖寫入這個文件,但不能。這是我的代碼。在Java中的ftp服務器上寫入文本文件

URL url = new URL("ftp://username:[email protected]/public/foler/a.txt;type=i"); 
URLConnection urlc = url.openConnection(); 
OutputStream os = urlc.getOutputStream(); // To upload 
OutputStream buffer = new BufferedOutputStream(os); 
ObjectOutput output = new ObjectOutputStream(buffer); 
output.writeChars("hello"); 
buffer.close(); 
os.close(); 
output.close(); 

回答

1

ObjectOutputStream類旨在寫入對象數據,因此它可以通過ObjectInputStreamsee here)重建。這不是寫文本文件。如果你所需要的只是寫入字符串以便更好地使用PrintStream

URL url = new URL("ftp://username:[email protected]/public/foler/a.txt;type=i"); 
URLConnection urlc = url.openConnection(); 
OutputStream os = urlc.getOutputStream(); // To upload 
OutputStream buffer = new BufferedOutputStream(os); 
PrintStream output = new PrintStream(buffer); 
output.print("hello"); 

buffer.close(); 
os.close(); 
output.close(); 
0

你用什麼庫?

我想你連接到FTP

我用這一個在我以前的項目

ApacheCommons FTPClient

隨時問,如果你有使用上述庫的問題時,必須使用正確的java庫。

0

看看Uploading to FTP using Java這個問題,然後看用戶Loša的答案。

是洛薩的回答是唯一缺少的是VAR BUFFER_SIZE作爲

final int BUFFER_SIZE = 1024; // or whatever size you think it should be 

和導入庫和基本類定義你在做什麼的定義。

一些簡單的搜索在這裏,或通過DuckDuckGo或谷歌會找到你要找的。

此外,您不是在問一個問題,而是說「這不起作用,我不知道爲什麼,請爲我解決。」