ftp复制文件 java,如何使用Java中的FTPClient在FTP服务器中复制文件?

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值