Secure File Transfer Protocol (SFTP) is a file protocol for transferring large files over the web. It builds on the File Transfer Protocol (FTP) and includes Secure Shell (SSH) security components. Show
In this tutorial, we'll discuss how to upload and download files from a remote server using SFTP in Java. We'll use three different libraries: JSch, SSHJ, and Apache Commons VFS. 2. Using JSchFirst, let's see how to upload and download files from a remote server using the JSch library. 2.1. Maven ConfigurationWe'll need to add the jsch dependency to our pom.xml:
The latest version of jsch can be found on Maven Central. 2.2. Setting Up JSchNow we'll set up JSch. JSch enables us to use either Password Authentication or Public Key Authentication to access a remote server. In this 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:
We can also generate the known_hosts file using the following command:
2.3. Uploading a File With JSchTo upload a file to the remote server, we'll use the method ChannelSftp.put():
In this example, the first parameter of the method represents the local file to be transferred, src/main/resources/sample.txt, while remoteDir is the path of the target directory at the remote server. 2.4. Downloading a File With JSchWe can also download a file from the remote server using ChannelSftp.get():
The remoteFile is the path of the file to be downloaded, and localDir represents the path of the target local directory. 3. Using SSHJNext, we'll use the SSHJ library to upload and download files from a remote server. 3.1. Maven ConfigurationFirst, we'll add the dependency to our pom.xml:
The latest version of sshj can be found on Maven Central. 3.2. Setting Up SSHJThen we'll set up the SSHClient. SSHJ also allows us to use Password or Public Key Authentication to access the remote server. We'll use the Password Authentication in our example:
3.3. Uploading a File With SSHJSimilar to JSch, we'll use the SFTPClient.put() method to upload a file to the remote server:
We have two new variables here to define:
3.4. Downloading a File With SSHJThe same goes for downloading a file from the remote server; we'll use SFTPClient.get(): 0And we'll add the two variables used above: 14. Using Apache Commons VFSFinally, we'll use Apache Commons VFS to transfer files to a remote server. In fact, Apache Commons VFS uses JSch library internally. 4.1. Maven ConfigurationWe need to add the commons-vfs2 dependency to our pom.xml: 2The latest version of commons-vfs2 can be found on Maven Central. 4.2. Uploading a File With Apache Commons VFSApache Commons VFS is a little different. We'll use a FileSystemManager to create FileObjects from our target files, then use the FileObjects to transfer our files. In this example, we'll upload a file by using the method FileObject.copyFrom(): 3Note that the local file path should be absolute, and the remote file path should start with sftp://username:[email protected] 4.3. Downloading a File With Apache Commons VFSDownloading a file from a remote server is very similar; we'll also use FileObject.copyFrom() to copy localFile from remoteFile: 45. ConclusionIn this article, we learned how to upload and download files from a remote SFTP server in Java. To do this, we used multiple libraries: JSch, SSHJ, and Apache Commons VFS. |