There is a use case, where I have to:
- Generate a file in my application, on an application server. Lets call this a machine as A*. (This is where my Java code runs, to generate files)
- From the application itself, I wish to transfer this file using SFTP to a white-listed Server B, at a fixed path (say /home/fixed/file.xlsx)
Point 1 and 2 are straight-forward.
There is now an external Server C (whose credentials I am provided with)
- Now, application is running on server A.
- File is now in Server B at ==> /home/fixed/file.xlsx.
- I have to transfer file from Server B to Server C, via SFTP.
How can I achieve this multiple hopped SFTP transfers?
(File is not needed on B, once sent successfully to C)
EDIT: Martin Prikryl's answer helped me achieve this.
@Override
  public void createOurChannel(Path lastModifiedBankFile) {
    LOG.info("Initiating SFTP for white-listed Server B for file: {}",
        lastModifiedBankFile);
    String host = properties.getServerBSftpHost();
    String port = properties.getServerBSftpPort();
    String password = properties.getServerBSftpPassword();
    String username = properties.getServerBSftpUsername();
    Session session = null;
    try {
      JSch ssh = new JSch();
      JSch.setConfig("StrictHostKeyChecking", "no");
      session = ssh.getSession(username, host, Integer.parseInt(port));
      session.setPassword(password);
      session.connect();
      this.sendFileToBank(lastModifiedBankFile, session, ssh);
    } catch (JSchException e) {
      LOG.error("Jsch Exception occurred while SFTP.", e);
    } finally {
      if (session != null && session.isConnected())
        session.disconnect();
      LOG.info("Successfully disconnected from SFTP. {}");
    }
  }
  @Override
  public void sendFileToBank(Path lastModifiedBankFile, Session session, JSch ssh) {
    Session sessionBank = null;
    Channel channelBank = null;
    ChannelSftp channelSftp = null;
    String host = properties.getBankSftpHost();
    String port = properties.getBankSftpPort();
    String password = properties.getBankSftpPassword();
    String username = properties.getBankSftpUsername();
    String bankSftpDir = properties.getBankSftpEmiDir();
    try {
      int portForwarded = 2222;
      session.setPortForwardingL(portForwarded, host, Integer.parseInt(port));
      sessionBank = ssh.getSession(username, "localhost", portForwarded);
      sessionBank.setPassword(password);
      sessionBank.connect();
      channelBank = sessionBank.openChannel("sftp");
      channelBank.connect();
      channelSftp = (ChannelSftp) channelBank;
      channelSftp.put(lastModifiedBankFile.toAbsolutePath().toString(),
          bankSftpDir + lastModifiedBankFile.getFileName().toString());
      channelSftp.exit();
    } catch (JSchException e) {
      LOG.error("Jsch Exception occurred while SFTP.", e);
    } catch (SftpException e) {
      LOG.error("SFTP Exception occurred while SFTP.", e);
    } finally {
      if (channelBank != null && channelBank.isConnected())
        channelBank.disconnect();
      if (sessionBank != null && sessionBank.isConnected())
        sessionBank.disconnect();
    }
  }
 
    