JSch is a Java library implementing SSH2 protocol, which allows us to connect and manipulate files on an SFTP server. In this tutorial, I will guide you all how to connect to an SFTP server using this library.
First, I will create a Maven project as an example:
JSch dependency is as follows:
1 2 3 4 5 |
<dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.54</version> </dependency> |
To connect to an SFTP server, we first need to initialize a JSch object:
1 |
JSch ssh = new JSch(); |
Any connection between the client and server requires a session. We can create a Session for our connection from the JSch object which we just created:
1 2 3 |
Session session = ssh.getSession(login, hostname, 22); session.setPassword(password); session.connect(); |
Inside:
- login is the name of the SFTP user.
- hostname is the address of the SFTP server.
- The password is the SFTP user’s password.
Now, if you run the above code you will get the following error:
1 2 3 4 5 |
Exception in thread "main" com.jcraft.jsch.JSchException: UnknownHostKey: 192.168.1.52 at com.jcraft.jsch.Session.checkHost(Unknown Source) at com.jcraft.jsch.Session.connect(Unknown Source) at com.jcraft.jsch.Session.connect(Unknown Source) at com.huongdanjava.jsch.Application.main(Application.java:24) |
The reason is that for SSH connections we need to confirm that the server we are connecting to, is secure. To bypass this validation, we can modify the default configuration of a Session.
1 2 3 |
java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); |
Run again, you will not get the error again.
Now from this Session object, we can open an SFTP channel to manipulate files on the SFTP server.
1 2 |
ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp"); sftp.connect(); |
By default, when opening a ChannelSftp, the default folder is the user directory.
We can get the current directory by calling:
1 |
sftp.pwd(); |
The entire code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package com.huongdanjava.jsch; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; import com.jcraft.jsch.SftpException; public class Application { public static void main(String[] args) throws JSchException, SftpException { String hostname = "192.168.51.24"; String login = "khanh"; String password = "123456"; java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); JSch ssh = new JSch(); Session session = ssh.getSession(login, hostname, 22); session.setConfig(config); session.setPassword(password); session.connect(); ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp"); sftp.connect(); System.err.println(sftp.pwd()); sftp.disconnect(); session.disconnect(); } } |
Richa
com.jcraft.jsch.JSchException: Algorithm negotiation fail
exception coming