How do I download a file from an SSH server using JSch SFTP?

To download a file from an SSH server using JSch SFTP, you can use the ChannelSftp class from the JSch library. Below is an example of how to achieve this:

Code Example: Downloading a file using JSch SFTP

The JSch library is used to establish an SSH connection to an SFTP server and transfer files. Here’s a step-by-step guide:

package org.kodejava.jsch;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

import java.io.FileOutputStream;

public class SFTPDownloadExample {
   public static void main(String[] args) {
      String sftpHost = "sftp.example.com";
      int sftpPort = 22;
      String sftpUser = "username";
      String sftpPassword = "password";
      String remoteFile = "/path/to/remote/file.txt";
      String localFile = "local-file-path.txt";

      Session session = null;
      Channel channel = null;
      ChannelSftp channelSftp = null;

      try {
         // Initialize JSch
         JSch jsch = new JSch();

         // Create session
         session = jsch.getSession(sftpUser, sftpHost, sftpPort);

         // Set the password
         session.setPassword(sftpPassword);

         // Configure strict host key checking (optional)
         session.setConfig("StrictHostKeyChecking", "no");

         // Connect the session
         System.out.println("Connecting to the SFTP server...");
         session.connect();
         System.out.println("Connected successfully.");

         // Open the SFTP channel
         channel = session.openChannel("sftp");
         channel.connect();
         channelSftp = (ChannelSftp) channel;

         // Download the file
         System.out.println("Downloading file...");
         channelSftp.get(remoteFile, localFile);
         System.out.println("File downloaded to: " + localFile);

      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         // Clean up resources
         if (channelSftp != null) {
            channelSftp.disconnect();
         }
         if (channel != null) {
            channel.disconnect();
         }
         if (session != null) {
            session.disconnect();
         }
      }
   }
}

Explanation of the Code

  1. Set up connection details: Set the SFTP server’s host, port, username, password, the path to the remote file, and the local file.
  2. JSch initialization:
    • Create a Session object with user credentials (host, port, username, and password).
    • Use session.setConfig("StrictHostKeyChecking", "no") to skip host key verification (use for testing; not recommended for production due to security risks).
    • Connect to the server using session.connect().
  3. Open the SFTP channel:
    • Open a channel to the server with session.openChannel("sftp").
    • Cast the channel to ChannelSftp and connect.
  4. Download the file:
    • Use ChannelSftp.get(remoteFile, localFile) to download the remote file to the specified local path.
  5. Clean up resources:
    • Disconnect the ChannelSftp, Channel, and Session objects to free up resources.

Output Example

If successful, the program outputs the following:

Connecting to the SFTP server...
Connected successfully.
Downloading file...
File downloaded to: local-file-path.txt

Note

  • If your SFTP server uses public/private keys, you can use jsch.addIdentity("path/to/private_key") before initiating the session instead of a password.
  • Always handle exceptions and manage resources carefully in a real-world application to ensure robustness.

This example should work to download files via SFTP in Java.


Maven Dependencies

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

Maven Central

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.