Transferring a File Through SFTP in Quarkus

Image for post
Image for post

In this tutorial we’ll discuss how to download and upload files from a remote server using SFTP in Quarkus.

we’ll use two different libraries: JSch and Quarkus JSch.

  1. Using JSch

First, let’s see how to download and upload files from a remote server using the JSch library.

1.1 Maven Configuration

We’ll need to add the jsch dependency to our pom.xml. The latest version of jsch can be found on Maven Central.

1.2 Add — initialize-at-run-time to application.properties

When using JSch library we’ll need to add a class com.jcraft.jsch to initialize at run time. If we’ll forget to add this class when we’ll build a native image we’ll get an error to follow the message below.

com.oracle.svm.core.util.UserError$UserException: No instances of java.net.Inet4Address are allowed in the image heap as this class should be initialized at image

2. Using Quarkus JSch

First, let’s see how to download and upload files from a remote server using the quarkus-jsch library.

2.1 Maven Configuration

We’ll need to add the quarkus-jsch dependency to our pom.xml. The latest version of jsch can be found on Maven Central.

3. Downloading a File With JSch or Quarkus JSch

Now, We’ll set up JSch or Quarkus JSch. JSch or Quarkus JSch enables us to use either password authentication or public key authentication to access a remote server. In the example, we’ll use password authentication.

In the example above, the remoteHost represents the name or IP address of the remote server (i.e. example.com). We can define the variables used in the test as:

In the example method getSFTPFile() above, we can also download a file from the remote server using ChannelSftp.get(). The filePath represents the path of the target local directory follow the example code below.

4. Uploading a File With JSch and Quarkus JSch

Now, We’ll set up JSch and Quarkus JSch. JSch and Quarkus JSch enables us to use either password authentication or public key authentication to access a remote server. In the example, we’ll use password authentication.

In the example above, the remoteHost represents the name or IP address of the remote server (i.e. example.com). We can define the variables used in the test as:

In the example method putSFTPFile() above, we can also upload a file from the remote server using ChannelSftp.put(). The filePath represents the path of the target local directory follow the example code below.

you can see our code here Lionel-P-Albus/quarkus-sftp

Thanks, everyone for reading if you have any questions please comment. The last one I’m sorry about my English language if I’m using the wrong word please recommend to me. 👏👏👏

Full-Stack Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store