Java SFTP | File Transfer Using SFTP in Java JSCH
In digital word transferring file client to server and vice-versa is a typical process because the size of file may be large or may be un authorize access. So, it become necessary to protect the file and data from authorization. The SFTP ensures the security while transferring the file over the network. In this section, we will discuss how to transfer file from a remote server to the local system and vice versa using SSH File Transfer Protocol (SFTP) in Java.
SFTP stands for SSH File Transfer Protocol. It has replaced the legacy FTP protocol. It runs over the SSH protocol. It supports the full security and authentication functionality of SSH. In couple of years, it will also take over the FTPS protocol. There is no dedicated SFTP port; SFTP uses the normal SSH port i.e. 22.
Features of SFTP
Many SSH clients support SFTP:
SFTP Libraries for Java Developers
Using JSch Library
JSch library provides the get() and put() method to transfer file between server and client. The put() method is used to transfer files from a local system to a remote server.
Add the jsch dependency to the pom.xml file.
It automatically downloads the jsch-0.1.55.jar file to the project directory.
We use put() method to transfer files from a local host to the server.
We use get() to download files from server to the local host.
Public and private keys authentication use Public Key Authentication with SSH
Let's see how to use keys in program.
let's see the following JSch example to transfer a file from the local system to a remote server 184.108.40.206, authenticate using an SSH password.
Note that the Jar file should be properly attached, else you will get error.