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?
解决方案
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);
在FTP服务器上尝试同时下载和上传文件会导致文件为空。正确的做法是先完全下载文件到临时文件或内存,然后再上传。或者可以使用两个不同的FTP连接来实现文件的复制与重命名操作。具体步骤包括下载文件到内存,然后通过第二个FTP连接将文件存储为新名称。
2366

被折叠的 条评论
为什么被折叠?



