2

I tried finding Java code for SFTP client using Apache MINA library but couldn't find it.

Could someone show me how to write a simple password authentication based SFTP client using Apache MINA library.

https://mina.apache.org/sshd-project/apidocs/org/apache/sshd/client/subsystem/sftp/SftpClient.html

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
ChiefAmay
  • 140
  • 3
  • 12
  • 1
    If you are unlucky finding Apache MINA examples, and you are allowed to use any other library, perhaps you could try [Jsch](http://www.jcraft.com/jsch/). You can find several examples here: https://stackoverflow.com/questions/14617/how-to-retrieve-a-file-from-a-server-via-sftp – aaguilera Mar 13 '19 at 08:48

1 Answers1

8

Based on the examples in the README.md in the root of apache-sshd-2.2.0-src package:

SshClient client = SshClient.setupDefaultClient();
// override any default configuration...
client.setSomeConfiguration(...);
client.setOtherConfiguration(...);
client.start();
try (ClientSession session = client.connect(user, host, port).verify(timeout).getSession()) {
    session.addPasswordIdentity(password);
    session.auth.verify(timeout);

    // User-specific factory
    try (SftpClient sftp = DefaultSftpClientFactory.INSTANCE.createSftpClient(session)) {
        // use sftp here
    }
}
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992