如何使用Java中的FTPClient在FTP服务器中复制文件? [英] How to copy a file in FTP server using FTPClient in Java?
问题描述
我有一个CSV文件,我需要将其复制并在同一路径中将其重命名为 。
FTP登录:
InputStream inputStream = ftpClient.retrieveFileStream(cvs_name +。csv);
ftpClient.storeFile(cvs_name2 +.csv,inputStream);
但是当我在服务器上验证文件时,它是空的。我如何复制文件并重命名它?
我相信你的代码无法工作。您无法同时通过一个FTP连接下载和上传文件。
您有两种选择: 完全下载文件(临时文件或内存)。 如何将ftp服务器上的文件复制到java中同一服务器上的目录?显示to memory解决方案。注意
outputStream.toByteArray()
调用。 FTPClient
)并在实例之间复制文件。
InputStream inputStream = ftpClient1。 retrieveFileStream(cvs_name +.csv);
ftpClient2.storeFile(cvs_name2 +.csv,inputStream);
I have a CSV file, and I need to copy it and rename it in the same path.
I tried this after the FTP login:
InputStream inputStream = ftpClient.retrieveFileStream(cvs_name +".csv");
ftpClient.storeFile(cvs_name2 + ".csv",inputStream);
But when I verify the file on the server, it's empty. How can I copy a file and rename it?
I believe your code cannot work. You cannot download and upload a file over a single FTP connection at the same time.
You have two options:
Download the file completely first (to a temporary file or to a memory).
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.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);
这篇关于如何使用Java中的FTPClient在FTP服务器中复制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!