0

Please refer to an existing question regarding the same Send files from one remote server using JSch to another server using JSch too

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?

Solution given for above Question is "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;
}

But My query is what if I want to directly transfer files from server1 to server2 without getting it on my local. This I can do using command "scp -rv /sourcefolder/text1 username@server2Hostname /destinationfolder/" but through Program it will work ONLY when public key is set up between server1 and server2 SO no Requirement to pass the password. But I need a solution when public key is Not set up and I have to use the Password, Am not able to figure out how to pass the password for server2 in this command OR if we can create parallel session/channel to server1 and server2 NOT Sure how can we do that. ANy Idea ?

ASR
  • 311
  • 2
  • 14

0 Answers0