How we can connect SFTP Server in Java?

We need to pull/put some files from/to SFTP server. How we can connect SFTP Server and do the same?

2 Likes

@radhika Kindly look into this.

To connect to an SFTP (SSH File Transfer Protocol) server and perform file operations in a Java Spring Boot application, you can use the JSch library, which is a popular Java library for working with SSH and SFTP. Here’s a step-by-step guide on how to achieve this:

  1. Add JSch Dependency:
    First, you need to add the JSch dependency to your Spring Boot project. You can do this by adding the following dependency to your pom.xml file:

    <dependency>
        <groupId>com.jcraft</groupId>
        <artifactId>jsch</artifactId>
        <version>0.1.55</version> <!-- Replace with the latest version available -->
    </dependency>
    
  2. Create a Service for SFTP Operations:
    Create a service or class that will handle the SFTP operations. Below is a basic example of a service class that connects to an SFTP server and performs file transfer operations:

    import com.jcraft.jsch.ChannelSftp;
    import com.jcraft.jsch.JSch;
    import com.jcraft.jsch.Session;
    import org.springframework.stereotype.Service;
    
    @Service
    public class SftpService {
    
        private String host = "sftp.example.com";
        private int port = 22;
        private String username = "your-username";
        private String password = "your-password";
    
        public void uploadFile(String localFilePath, String remoteDirectory) {
            JSch jsch = new JSch();
            Session session = null;
    
            try {
                session = jsch.getSession(username, host, port);
                session.setPassword(password);
                session.setConfig("StrictHostKeyChecking", "no");
                session.connect();
    
                ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
                channel.connect();
                channel.put(localFilePath, remoteDirectory);
                channel.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (session != null && session.isConnected()) {
                    session.disconnect();
                }
            }
        }
    
        public void downloadFile(String remoteFilePath, String localDirectory) {
            // Implement the download functionality in a similar way to uploadFile
        }
    }
    
  3. Inject the SFTP Service:
    Inject the SftpService into your Spring components (controllers, services, etc.) where you need to perform SFTP operations.

  4. Usage:
    You can now use the SftpService to upload or download files from the SFTP server.

    @Autowired
    private SftpService sftpService;
    
    // Example: Upload a file
    sftpService.uploadFile("/local/path/to/file.txt", "/remote/directory/");
    
    // Example: Download a file
    sftpService.downloadFile("/remote/file.txt", "/local/directory/");
    

Make sure to replace the placeholders for host, username, password, and file paths with your specific SFTP server and file locations. Additionally, handle exceptions and error cases according to your application’s requirements.

2 Likes