nirmalakumarsahu

Spring Boot with SFTP Integration

πŸ“„ Articles πŸ‘€ My Profile

Spring Boot SFTP

Spring Framework Framework


πŸ“‘ Index


πŸ”‘ What is SFTP

SFTP stands for Secure File Transfer Protocol (also known as SSH File Transfer Protocol).

✨ It is a network protocol used to transfer files securely over a protected channel.

πŸ”“ Unlike FTP (File Transfer Protocol), which sends data (including usernames and passwords) in plain text,

πŸ” SFTP encrypts both commands and data using SSH (Secure Shell), making it much more secure.

πŸ“‚βž‘οΈπŸ”’ File moves safely from one system to another with full encryption.

πŸ”‘ Key Features of SFTP

πŸ”„ SFTP vs FTP vs FTPS

Feature FTP (File Transfer Protocol) FTPS (FTP Secure) SFTP (SSH File Transfer Protocol)
Security No encryption (plain text) SSL/TLS encryption SSH encryption (very secure)
Port 21 (plus random data ports) 21 (plus TLS/SSL) 22 (single port)
Auth Username + Password Username + Password + Certs Username + Password / SSH keys
Protocol FTP only FTP + TLS Built on SSH

πŸ› οΈ Example Use Cases

βš™οΈ How SFTP Works

  1. Connection Initialization

    • The client (you) tries to connect to the SFTP server on port 22 (default for SSH/SFTP).
    • Example:

      sftp user@server.com
      
  2. Authentication

    • The server authenticates the client using either:

      • Password authentication (username + password), or
      • Public/Private key authentication (using .pem or .ppk files).
    • Keys are more secure because the private key never leaves your machine.

  3. Secure Channel Setup

    • Once authenticated, an SSH encrypted tunnel is established between client and server.
    • This ensures all commands and file data are encrypted.
  4. File Operations

    • Over the secure tunnel, the client can issue commands like:

      • ls β†’ list files
      • cd β†’ change directory
      • get file.txt β†’ download file
      • put report.csv β†’ upload file
    • Unlike FTP, these commands and the file contents are encrypted.

  5. Integrity & Acknowledgement

    • SFTP ensures that files are transferred completely.
    • If a transfer is interrupted, it can resume from where it left off.
  6. Disconnection

    • After the work is done, the client closes the session.
    • Example:

      bye
      

      πŸ–₯️ Example in Action (Linux/Windows Command Line)

# Connect to SFTP server
sftp -i ~/.ssh/id_rsa user@host.com

# Inside SFTP prompt
sftp> ls
sftp> cd /remote/path
sftp> put localfile.txt
sftp> get remotefile.csv
sftp> bye

πŸ” In short: SFTP works by combining SSH security + file transfer commands in one protocol, so every action and data packet is encrypted and safe.

πŸ” Back to Top


πŸ› οΈ Why Use SFTP in Spring Boot?

In enterprise systems, you often need to:

Spring Boot applications integrate with SFTP servers to handle these scenarios.

πŸ“¦ Library for SFTP in Spring Boot

We use JSch (Java Secure Channel) library:

Maven Central

<dependency>
    <groupId>com.github.mwiede</groupId>
    <artifactId>jsch</artifactId>
    <version>0.2.17</version>
</dependency>

πŸ”‘ Key Benefits of Using SFTP in Spring Boot

πŸ” Back to Top


πŸš€ Implementation

πŸ—οΈ Technology Stack

πŸ–₯️ Backend

πŸ“Š API Documentation

πŸ›’οΈ Database

πŸ“‚ File Transfer (SFTP)

πŸ› οΈ Build & Dependency Management

βš™οΈ Utilities

πŸ“‚ Project Structure

spring-boot-sftp-integration
│── πŸ“‚ src/
β”‚   └── πŸ“‚ main/
β”‚       β”œβ”€β”€ πŸ“‚ java/
β”‚       β”‚   └── πŸ“‚ com/sahu/springboot/basics/
β”‚       β”‚       β”œβ”€β”€ πŸ“‚ config/
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ OpenApiConfig.java
β”‚       β”‚       β”‚   └── πŸ“„ OpenApiProperties.java
β”‚       β”‚       β”‚
β”‚       β”‚       β”œβ”€β”€ πŸ“‚ constant/
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ ApiStatus.java
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ AppConstants.java
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ AuthenticationType.java
β”‚       β”‚       β”‚   └── πŸ“„ KeyFormat.java
β”‚       β”‚       β”‚
β”‚       β”‚       β”œβ”€β”€ πŸ“‚ controller/rest/
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ SftpConfigRestController.java
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ SftpFileRestController.java
β”‚       β”‚       β”‚   └── πŸ“„ SftpKeyUtilRestController.java
β”‚       β”‚       β”‚
β”‚       β”‚       β”œβ”€β”€ πŸ“‚ dto/
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ ApiResponse.java
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ SftpConfigRequest.java
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ SftpConfigResponse.java
β”‚       β”‚       β”‚   └── πŸ“„ SftpDirectoryResponse.java
β”‚       β”‚       β”‚
β”‚       β”‚       β”œβ”€β”€ πŸ“‚ exception/
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ GlobalExceptionHandler.java
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ InvalidSftpKeyFileException.java
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ SftpConfigAlreadyExistException.java
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ SftpConfigNotFoundException.java
β”‚       β”‚       β”‚   └── πŸ“„ SftpException.java
β”‚       β”‚       β”‚
β”‚       β”‚       β”œβ”€β”€ πŸ“‚ model/
β”‚       β”‚       β”‚   └── πŸ“„ SftpConfig.java
β”‚       β”‚       β”‚
β”‚       β”‚       β”œβ”€β”€ πŸ“‚ operation/
β”‚       β”‚       β”‚   └── πŸ“„ SftpConnectionHandler.java
β”‚       β”‚       β”‚
β”‚       β”‚       β”œβ”€β”€ πŸ“‚ repository/
β”‚       β”‚       β”‚   └── πŸ“„ SftpConfigRepository.java
β”‚       β”‚       β”‚
β”‚       β”‚       β”œβ”€β”€ πŸ“‚ service/
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“‚ impl/
β”‚       β”‚       β”‚   β”‚   β”œβ”€β”€ πŸ“„ FileReaderServiceImpl.java
β”‚       β”‚       β”‚   β”‚   └── πŸ“„ SftpConfigServiceImpl.java
β”‚       β”‚       β”‚   β”‚
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“‚ util/
β”‚       β”‚       β”‚   β”‚   └── πŸ“„ SftpConfigUtil.java
β”‚       β”‚       β”‚   β”‚
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ FileReaderService.java
β”‚       β”‚       β”‚   └── πŸ“„ SftpConfigService.java
β”‚       β”‚       β”‚
β”‚       β”‚       β”œβ”€β”€ πŸ“‚ util/
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ AseCryptUtil.java
β”‚       β”‚       β”‚   └── πŸ“„ SftpKeyConvertorUtil.java
β”‚       β”‚       β”‚
β”‚       β”‚       β”œβ”€β”€ πŸ“‚ validation/
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ PortValidator.java
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ RemoteDirectoryValidator.java
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ SftpConfigAuthValidator.java
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ ValidPort.java
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ ValidRemoteDirectory.java
β”‚       β”‚       β”‚   └── πŸ“„ ValidSftpConfigAuth.java
β”‚       β”‚       β”‚
β”‚       β”‚       └── πŸ“„ SpringBootSftpIntegrationApplication.java
β”‚       β”‚
β”‚       └── πŸ“‚ resources/
β”‚           └── πŸ“„ application.yml
β”‚
│── πŸ“„ docker-compose.yml
└── πŸ“„ pom.xml

πŸ”— Code Repository

You can find the complete code repository for this project on GitHub:

GitHub - spring-boot-sftp-integration

πŸš€ To Run the Spring Boot Application

1️⃣ 🐳 Using Docker Compose (for MySQL container)

docker-compose up -d

βœ… This starts MySQL in a container (-d = detached mode). πŸ” Verify with:

docker ps

πŸ“Œ DB is now available at localhost:3307. πŸ”‘ Credentials (username, password, DB name) are in docker-compose.yml.

2️⃣ πŸ’» Run Directly in IntelliJ IDEA

  1. πŸ“‚ Open the Spring Boot project in IntelliJ.
  2. ▢️ In Project Explorer, right-click the main class: SpringBootSftpIntegrationApplication.java
  3. Select Run β€˜SpringBootSftpIntegrationApplication’.
  4. 🐞 For debugging, click the Debug button instead of Run.
  5. 🌐 App will start on http://localhost:9897.

3️⃣ ⚑ Run with Maven Command (CLI)

πŸ” Back to Top


πŸŽ₯ Video Reference

For a detailed running and demonstration of the application walkthrough,
watch the following YouTube video:

Watch the video

πŸ” Back to Top

πŸ“– Read More ➑️