Open In App

Connecting to an SFTP Server using Java JSch Library

Improve
Improve
Like Article
Like
Save
Share
Report

SFTP (Secure File Transfer Protocol) is a secure way to transfer files between a client and a server. It is similar to FTP (File Transfer Protocol) but is more secure as it uses SSH (Secure Shell) to encrypt the data being transferred. In this article, we will learn how to connect to an SFTP server using the Java programming language. To connect to an SFTP server in Java, we will use the JSch library. JSch is a Java implementation of SSH2. It allows us to connect to an SFTP server, and perform various operations like uploading and downloading files, creating and deleting directories, etc.

First, let’s create a new Java project and add the JSch library to its classpath. You can download the JSch library from its official website or use a dependency management tool like Maven to add it to your project.

Connecting to an SFTP server using Java can be done using the JSch library. JSch is a Java implementation of SSH and SFTP. It allows you to connect to an SFTP server and perform various operations such as uploading, downloading, and deleting files.

Step by Step Process

Step 1. First, you need to download and add the JSch library to your project. You can do this by adding the following dependency to your build file.

<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.55</version>
</dependency>

Step 2. Once you have added the dependency, you can start connecting to the SFTP server. The following code snippet shows an example of how to connect to an SFTP server using JSch.

Java




import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
  
public class SftpExample {
    public static void main(String[] args) {
        String host = "sftp.example.com";
        String user = "username";
        String password = "password";
        int port = 22;
          
        try {
            JSch jsch = new JSch();
            Session session = jsch.getSession(user, host, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();
              
            // Perform SFTP operations here
           
            session.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


In the above example, we are connecting to an SFTP server at “sftp.example.com” using the username “username” and password “password”. The SFTP server is running on port 22, which is the default port for SFTP.

Step 3. Once you have established a connection to the SFTP server, you can perform various operations such as uploading, downloading and deleting files using the session object. Here is an example of how to upload a file to the SFTP server.

Java




import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
  
public class SftpExample {
    public static void main(String[] args) {
        // ...
        try {
            // ...
            Channel channel = session.openChannel("sftp");
            channel.connect();
            ChannelSftp sftp = (ChannelSftp) channel;
              
            File file = new File("localFile.txt");
            sftp.put(new FileInputStream(file), "remoteFile.txt");
              
            sftp.disconnect();
            channel.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


In this example, we are uploading a file named “localFile.txt” to the SFTP server and saving it as “remoteFile.txt”. That’s it! With the JSch library, you can easily connect to an SFTP server and perform various operations using Java.



Last Updated : 03 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads