How to copy a file in FTP server using FTPClient i

2020-02-06 17:05发布

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条回答
走好不送
2楼-- · 2020-02-06 17:50

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);
    
查看更多
登录 后发表回答