[英]Writing into text file on ftp server in Java
我的ftp服務器上有文本文件。 我正在嘗試寫入此文件,但不能。 這是我的代碼。
URL url = new URL("ftp://username:pass@thunder.cise.ufl.edu/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();
ObjectOutputStream
類旨在寫入對象數據,以便可以通過ObjectInputStream
對其進行重構( 請參見此處 )。 它不是用於編寫文本文件。 如果您需要編寫String以更好地進行流傳輸,請使用PrintStream
URL url = new URL("ftp://username:pass@thunder.cise.ufl.edu/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();
看一下使用Java上傳到FTP的問題,並查看Loša用戶的回答。
Loša答案唯一缺少的是var BUFFER_SIZE
的定義為
final int BUFFER_SIZE = 1024; // or whatever size you think it should be
並導入您正在執行的操作的庫和基本類定義。
在這里進行簡單的搜索,或者通過DuckDuckGo或Google進行搜索,就會找到您想要的東西。
另外,您問的不是問題,而是說“這行不通,我也不知道為什么。為我修復它”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.