簡體   English   中英

用Java在ftp服務器上寫入文本文件

[英]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();

您使用什么圖書館?

我認為您在連接FTP時必須使用正確的Java庫

我在以前的項目中使用了這個

ApacheCommons FTPClient

隨時詢問您使用上述庫是否有問題。

看一下使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM