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

前端 未结 1 1798
感情败类
感情败类 2020-12-10 22:33

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 = ftpClie         


        
1条回答
  •  温柔的废话
    2020-12-10 23:29

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

    0 讨论(0)
提交回复
热议问题