0

I would like to send files from my first remote server to another one:

public boolean uploadFile() throws JSchException, SftpException {
    ChannelSftp channelSftpA = createChannelSftp();
    ChannelSftp channelSftpB = createChannelSftp();
    channelSftpA.connect();
    channelSftpB.connect();

    localFilePath = "/data/upload/readme.txt";
    remoteFilePath = "/bingo/pdf/";

    channelSftpA.cd(localFilePath);
    channelSftpA.put(localFilePath + "readme.txt", remoteFilePath + "readme.txt");

But it doesn't work. Should I put channelB.put into my first channelA.put?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • See also [Send files directly from remote A to remote B from local using JSch?](https://stackoverflow.com/q/74397232/850848) – Martin Prikryl Feb 20 '23 at 09:38

1 Answers1

2

If I understood your question correct, you code will be run from third server, for transferring file you should get file from server A, and that put on server B. By the way users under which you are going to download and upload files should have access to specified folders!

private boolean transferFile() throws JSchException, SftpException {
    ChannelSftp channelSftpA = createChannelSftp();
    ChannelSftp channelSftpB = createChannelSftp();
    channelSftpA.connect();
    channelSftpB.connect();

    String fileName = "readme.txt";
    String remoteFilePathFrom = "/folderFrom/";
    String remoteFilePathTo = "/folderTo/";

    InputStream srcInputStream = channelSftpA.get(remoteFilePathFrom + fileName);
    channelSftpB.put(srcInputStream, remoteFilePathTo + fileName);
    System.out.println("Transfer has been completed");

    channelSftpA.exit();
    channelSftpB.exit();
    return true;
}
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
saver
  • 2,541
  • 1
  • 9
  • 14