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

廉价感情. 提交于 2019-11-27 07:26:44

问题


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?


回答1:


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);
    


来源:https://stackoverflow.com/questions/37663865/how-to-copy-a-file-in-ftp-server-using-ftpclient-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!