Reputation: 61
I want to copy a file from one FTP server to another FTP server (two different hosts).
Files.copy(
new File(channelSftp.realpath(fileName)).toPath(),
new File(channelSftp2.realpath(fileName)).toPath());
It is giving java.nio.file.NoSuchFileException
Can someone please help me with this.
Upvotes: 2
Views: 3720
Reputation: 202168
In general, you cannot directly transfer a file from one remote FTP server to another remote FTP server, if FTP protocol is the only way you can access the machines.
There's FXP protocol that allows that, but that's typically not allowed on most FTP servers, as they are usually configured not to accept data connections from IP addresses that are different to client IP address.
The only reliable solution is to download the file to local machine and upload it to the other FTP server.
You can basically use the same solutions as are shown in these answers for coping file from one directory to another on the same FTP server:
(but for both you obviously need two instances of FTPClient
)
If you have another access to one of the servers, like SSH, you can of course automatically login to the server and then run FTP on it to upload/download to/from the other server.
Though despite your wording, your question seems to actually be about SFTP, not FTP. Those are two completely different protocols.
SFTP does not support transfer between servers at all. See
So again, you will typically end up downloading the file to a local machine and uploading to the other server.
Again, use the same code as shown there for coping file from one SFTP directory to another, just use two different hosts:
Or if you have a shell access to one of the server, you can programmaticaly execute some SFTP client to upload/download a file to/from the other server. But that's a completely different question.
Upvotes: 1
Reputation: 61
I got the solution, thanks for the help.
it is working with following code:
sftp2.put(sftp.get(fileName),fileName);
Upvotes: 1