<< Back to tutorials

Quick Storage API in Java with the SFTP protocol

Published {$created} by Carsten Blum


Java may not be the first language you think of when working with file transfers, but Java is actually widely used in enterprise environments, and industries like fintech and insurance often use cloud FTP storage to transfer files between various parties. For fintech this could be daily currencies and trade volumes. With the right library, it’s easy to use SFTP as a lightweight Storage API in Java. When it comes to SFTP in Java, the classic JSch library is no longer actively maintained. Instead, a modern and reliable option is Apache MINA SSHD.

With MINA SSHD’s SFTP client, you can use Java to implement a lightweight cloud Storage API that’s standards-based, secure, and easy to extend.


In this tutorial, we’ll create a simple text file and upload it securely to ftpGrid over SFTP using Apache MINA SSHD.


I you want to follow along with this tutorial, please use our getting started with FTP/SFTP guide to create your account. To read more in-depth about ftpGrid managed FTP/SFTP cloud hosting checkout our our guide to ftpGrid's managed hosting. Finally, if you need help creating SSH keys, follow our SSH key creation tutorial.


Basic knowledge with the Java programming language is assumed in this tutorial. Setting up paths and libraries can be very fickly in Java, compared to languages like Go, which just seems to work, so pay close attention to paths and naming of files etc.



Requirements

  • Java 11 or newer

  • Maven or Gradle for dependency management

  • An ftpGrid account with SFTP enabled

  • Library: org.apache.sshd:sshd-sftp



Setting up you maven project

In your tutorial directory create a file called pom.xml and add below content:

<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                            http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ftpgrid</groupId>
    <artifactId>java-sftp-example</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.sshd</groupId>
            <artifactId>sshd-core</artifactId>
            <version>2.16.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.sshd</groupId>
            <artifactId>sshd-sftp</artifactId>
            <version>2.16.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                <mainClass>com.ftpgrid.UploadExample</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>



Generate and Upload a File (Password Auth)

Here’s a Java example using Apache MINA SSHD to create hello-from-java.txt, ensure /uploads exists, and upload the file.

In our example this file must be placed in the directory: src/main/java/com/ftpgrid/UploadExample.java


package com.ftpgrid;

import org.apache.sshd.client.SshClient; 
import org.apache.sshd.client.session.ClientSession; 
import org.apache.sshd.sftp.client.SftpClient; 
import org.apache.sshd.sftp.client.SftpClientFactory;

import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;

public class UploadExample {
    public static void main(String[] args) throws Exception {
        String host = "edge1.ftpgrid.com";
        int port = 22;
        String user = "PREFIX.username";
        String password = "Your password";
        
        // Step 1: Generate a local file
        Path localFile = Path.of("hello-from-java.txt");
        try (FileWriter fw = new FileWriter(localFile.toFile())) {
            fw.write("Hello from Java via ftpGrid SFTP!\n");
        }

        // Step 2: Setup SSH client
        try (SshClient client = SshClient.setUpDefaultClient()) {
            client.start();

            try (ClientSession session = client.connect(user, host, port)
                    .verify(10, TimeUnit.SECONDS)
                    .getSession()) {

                session.addPasswordIdentity(password);
                session.auth().verify(10, TimeUnit.SECONDS);

                // Step 3: Open SFTP client
                try (SftpClient sftp = SftpClientFactory.instance().createSftpClient(session)) {

                    // Step 4: Ensure remote directory exists
                    String remoteDir = "/uploads";
                    try {
                        sftp.stat(remoteDir);
                    } catch (IOException e) {
                        sftp.mkdir(remoteDir);
                    }

                    // Step 5: Upload the file
                    try (var out = sftp.write(remoteDir + "/hello-from-java.txt")) {
                        Files.copy(localFile, out);
                    }

                    System.out.println("Uploaded file to " + remoteDir + "/hello-from-java.txt");
                }
            }
        }
    }
}


Run with Maven:

mvn compile exec:java


Tip if you don't have maven installed, you can easily install on Linux og Mac using this command:


Mac, with brew installed:
brew install mvn

Linux, for instance on Debian:
sudo apt install maven


Using SSH Keys (Recommended)

Apache MINA SSHD also supports key-based authentication. Just load your private key instead of using password.


session.addPublicKeyIdentity(Path.of(System.getProperty("user.home"), ".ssh", "id_ed25519"));
session.auth().verify(10, TimeUnit.SECONDS);



Why This Matters

With Apache MINA SSHD, Java developers now have a modern, fully supported way to use SFTP as a Storage API.It’s ideal for:

  • Automated backups

  • Integrations between enterprise systems

  • Secure file pipelines

At ftpGrid, we make sure your data stays encrypted in transit and at rest — and with the right client library, Java applications can plug into it effortlessly.


Create your free ftpGrid account →



Next in the Quick Storage API series: We’ll explore the same example in C#.



Signup now
© 2025 ftpGrid

ftpGrid ApS
Branebjerg 24
DK-5471
Gamby
Denmark

Looking for an all-in-one time tracking, timesheet, and invoicing solution - visit our Devanux sister company Nureti at https://nureti.com.

Preview Devanux’s upcoming project Pictoguide – a visual support tool designed to bring structure and clarity to people with ASD.