2014-01-21 84 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或谷歌会找到你要找的。

此外,您不是在问一个问题,而是说“这不起作用,我不知道为什么,请为我解决。”