简体   繁体   English

如何使用Java中的FTPClient在FTP服务器中复制文件?

[英]How to copy a file in FTP server using FTPClient in Java?

I have a CSV file, and I need to copy it and rename it in the same path . 我有一个CSV文件,我需要将其复制并在同一路径中重命名。

I tried this after the FTP login: 我在FTP登录后尝试了此操作:

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. 您不能同时通过单个FTP连接下载和上传文件。

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? 如何将ftp服务器上的文件复制到java中同一服务器上的目录的可接受答案 shows the "to memory" solution. 显示了“存储”解决方案。 Note the outputStream.toByteArray() call. 请注意outputStream.toByteArray()调用。

  • Open two connections (two instances of the FTPClient ) and copy the file between the instances. 打开两个连接( FTPClient两个实例),然后在这些实例之间复制文件。

     InputStream inputStream = ftpClient1.retrieveFileStream(cvs_name + ".csv"); ftpClient2.storeFile(cvs_name2 + ".csv", inputStream); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM