1

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?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Pablo Glez
  • 306
  • 1
  • 7
  • 20
  • 1
    This question has been answered [here](http://stackoverflow.com/questions/11202215/how-to-copy-a-file-on-the-ftp-server-to-a-directory-on-the-same-server-in-java) – Ohad Benita Jun 06 '16 at 18:06
  • I already saw it, but in that case it's in different directory, I think if I use it in the same directory it will overwrite the original file, and that's not what I need – Pablo Glez Jun 06 '16 at 18:19

1 Answers1

2

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);
    
Community
  • 1
  • 1
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992