問題描述
我有一個(gè) CSV 文件,我需要復(fù)制它并在同一路徑中重命名它.
I have a CSV file, and I need to copy it and rename it in the same path.
我在 FTP 登錄后嘗試了這個(gè):
I tried this after the FTP login:
InputStream inputStream = ftpClient.retrieveFileStream(cvs_name +".csv");
ftpClient.storeFile(cvs_name2 + ".csv",inputStream);
但是當(dāng)我在服務(wù)器上驗(yàn)證文件時(shí),它是空的.如何復(fù)制文件并重命名?
But when I verify the file on the server, it's empty. How can I copy a file and rename it?
推薦答案
我相信你的代碼不能工作.您不能同時(shí)通過一個(gè) FTP 連接下載和上傳文件.
I believe your code cannot work. You cannot download and upload a file over a single FTP connection at the same time.
你有兩個(gè)選擇:
首先將文件完全下載(到臨時(shí)文件或內(nèi)存中).
Download the file completely first (to a temporary file or to a memory).
如何將 ftp 服務(wù)器上的文件復(fù)制到 java 中同一服務(wù)器上的目錄?顯示記憶"解決方案.注意 outputStream.toByteArray()
調(diào)用.
The accepted answer to How to copy a file on the ftp server to a directory on the same server in java? shows the "to memory" solution. Note the outputStream.toByteArray()
call.
打開兩個(gè)連接(FTPClient
的兩個(gè)實(shí)例)并在實(shí)例之間復(fù)制文件.
Open two connections (two instances of the FTPClient
) and copy the file between the instances.
InputStream inputStream = ftpClient1.retrieveFileStream(cvs_name + ".csv");
ftpClient2.storeFile(cvs_name2 + ".csv", inputStream);
這篇關(guān)于如何在 Java 中使用 FTPClient 復(fù)制 FTP 服務(wù)器中的文件?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!