SFTP is a Secure File Transfer Protocol used for transferring large files over the internet. It is based on the FTP foundation and provides Secure Shell components to transmit data over the secure channel. It uses a password or public-key authentication and can also encrypt data communications between client and server.
You can use the JSch library to transfer a file through SFTP in Java. JSch also called “Java Secure Shell” is a Java implementation of SSH2. It allows you to connect to the Java application via an SSH server then transfer files. In addition, you can use the JSch library to copy files to remote machines without manual intervention.
You can also use SSHJ, and Apache Commons VFS library to upload and download files from a remote server using SFTP in Java. SSHJ is a new library and provides a Java API for SSH. Apache Commons VFS provides a single API for accessing various file systems such as SFTP.
This post will explain how to transfer a file to and from a remote SFTP server using three different libraries, JSch, SSHJ, and Apache Commons VFS.
Use JSch to Transfer a File from SFTP
This section will show you how to download and upload a file from a remote SFTP server using the JSch library.
Configure Maven
First, you will need to add the following jsch dependencies to your POM.xml file.
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
You can download the latest version of jsch from Maven Central.
Configure JSch
JSch uses password authentication or public-key authentication to connect the remote SFTP server. In this example, we will configure JSch to use password authentication.
You can define your SFTP host, user, and password as shown below:
private ChannelSftp setupJsch() throws JSchException {
private String remoteHost = "ftp.example.com";
private String username = "ftpuser";
private String password = "ftppassword";
JSch jsch = new JSch();
jsch.setKnownHosts("/Users/hitesh/.ssh/known_hosts");
Session jschSession = jsch.getSession(username, remoteHost);
jschSession.setPassword(password);
jschSession.connect();
return (ChannelSftp) jschSession.openChannel("sftp");
}
After defining your remote SFTP connection, you can proceed to the next step.
Upload a File using SFTP
After configuring the SFTP connection, we will upload a file to the SFTP server. There are several methods to upload a file to the SFTP server. In this example, we will use ChannelSftp.put() method for uploading a file.
Here is the sample code to upload a file to the SFTP server:
public void whenUploadFileUsingJsch_thenSuccess() throws JSchException, SftpException {
ChannelSftp channelSftp = setupJsch();
channelSftp.connect();
String localFile = "/mnt/file.txt";
String remoteDir = "/data/";
channelSftp.put(localFile, remoteDir + "jschFile.txt");
channelSftp.exit();
}
Where:
- localFile is the path of the file that you want to upload.
- remoteDir is the path of the remote directory on the SFTP server.
Download a File using SFTP
You can also use the ChannelSftp.get() method to download a file from the remote SFTP server.
Here is the code to download a file:
public void whenDownloadFileUsingJsch_thenSuccess() throws JSchException, SftpException {
ChannelSftp channelSftp = setupJsch();
channelSftp.connect();
String remoteFile = "remotefile.txt";
String localDir = "/backup/";
channelSftp.get(remoteFile, localDir + "jschFile.txt");
channelSftp.exit();
}
Where:
- remoteFile is the path of the file on the remote SFTP server that you want to download.
- localDir is the path of your local system.
Use SSHJ to Transfer a File from SFTP
This section will show you how to download and upload a file from a remote SFTP server using SSHJ.
Configure Maven
First, you will need to add the following SSHJ dependencies to your POM.xml file.
<dependency>
<groupId>com.hierynomus</groupId>
<artifactId>sshj</artifactId>
<version>0.27.0</version>
</dependency>
You can download the latest version of SSHJ from Maven Central.
Configure SSHJ
First, you will need to configure SSHJ to use password authentication to connect the SFTP server.
You can use the following code for the SFTP connection.
private SSHClient setupSshj() throws IOException {
private String remoteHost = "ftp.example.com";
private String username = "ftpuser";
private String password = "ftppassword";
SSHClient client = new SSHClient();
client.addHostKeyVerifier(new PromiscuousVerifier());
client.connect(remoteHost);
client.authPassword(username, password);
return client;
}
Replaced remoteHost, username, and password value with your SFTP credentials.
Upload a File Using SFTP
You can use SFTPClient.put() method to upload a file to the remote SFTP server.
public void whenUploadFileUsingSshj_thenSuccess() throws IOException {
private String localFile = "/mnt/localfile.txt";
private String remoteDir = "/remotedirectory/";
SSHClient sshClient = setupSshj();
SFTPClient sftpClient = sshClient.newSFTPClient();
sftpClient.put(localFile, remoteDir + "sshjFile.txt");
sftpClient.close();
sshClient.disconnect();
}
Where:
- localFile is the path of your local file.
- remoteDir is the path of your remote directory.
Download a File Using SFTP
If you want to download a file from the remote SFTP server use the SFTPClient.get() method as shown below:
public void whenDownloadFileUsingSshj_thenSuccess() throws IOException {
private String remoteFile = "/mnt/remote.txt";
private String localDir = "/data/";
SSHClient sshClient = setupSshj();
SFTPClient sftpClient = sshClient.newSFTPClient();
sftpClient.get(remoteFile, localDir + "sshjFile.txt");
sftpClient.close();
sshClient.disconnect();
}
Where:
- remoteFile is the path of the remote file on the SFTP server.
- localDir is the path of the local directory.
Use Apache Commons VFS to Transfer a File from SFTP
This section will show you how to use Apache Commons VFS library to transfer a file to and from the SFTP server.
Configure Maven
First, you will need to add the following commons-vfs2 dependencies to your POM.xml file.
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-vfs2</artifactId>
<version>2.4</version>
</dependency>
You can download the latest version of commons-vfs2 from Maven Central.
Upload a File Using SFTP
Apache Commons VFS uses FileObject.copyFrom() method to upload a file to the remote SFTP server.
Use the following code to upload a file:
public void whenUploadFileUsingVfs_thenSuccess() throws IOException {
private String remoteHost = "ftp.example.com";
private String username = "ftpuser";
private String password = "ftppassword";
FileSystemManager manager = VFS.getManager();
FileObject local = manager.resolveFile(
System.getProperty("user.dir") + "/" + localFile);
FileObject remote = manager.resolveFile(
"sftp://" + username + ":" + password + "@" + remoteHost + "/" + remoteDir + "vfsFile.txt");
remote.copyFrom(local, Selectors.SELECT_SELF);
local.close();
remote.close();
}
Download a File Using SFTP
Apache Commons VFS uses FileObject.copyFrom() method to download a file from the remote SFTP server to your local system.
Use the following code to download a file:
public void whenDownloadFileUsingVfs_thenSuccess() throws IOException {
private String remoteHost = "ftp.example.com";
private String username = "ftpuser";
private String password = "ftppassword";
FileSystemManager manager = VFS.getManager();
FileObject local = manager.resolveFile(
System.getProperty("user.dir") + "/" + localDir + "vfsFile.txt");
FileObject remote = manager.resolveFile(
"sftp://" + username + ":" + password + "@" + remoteHost + "/" + remoteFile);
local.copyFrom(remote, Selectors.SELECT_SELF);
local.close();
remote.close();
}
Conclusion
The above guide explained how to download and upload a file from a remote SFTP user using three different Java libraries. You can now use any of them as per your requirements.
SFTP with Java FAQs
How install SFTP file in Java?
In order to use SFTP commands inside Java, you need to download the JSch library from Maven Central. With this group of functions available locally, you can then include the library in your environment by adding a dependency in your POM.xml file. This addition takes the form:
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
With this step completed you can use the get() method to retrieve files from remote computers and the put() method to send a file.
What ports does SFTP use?
SFTP only needs one channel to operate and so only requires one port. The SFTP system is based on SSH and so it uses the same port as SSH. This is TCP port 22. If you want to contrast this to FTPS, the FTPS service is based on SSL, which means that its security is managed by Transport Layer Security (TLS). Thus, FTPS operates over the same ports as SSL. This requires two connections: one is the control channel, which operates over TCP port 990 and the other is the data transfer channel, which uses port 989. Both of these are TCP.
What is FTPS vs SFTP?
FTPS is a secure file transfer protocol that uses SSL for connection security. As SSL is now provided by TLS, the FTPS system is based on Transport Layer Security. TLS opens two connections, which are a control channel and a data channel. SFTP is secured by the Secure Shell (SSH). This requires only one channel, which carries both commands and data. Both systems are well established and widely used and both are considered secure file transfer systems. The relative benefits of each system are that FTPS is believed to be easier to manage because its SSL security system is integrated into many other secure connection services that your computer uses, such as HTTPS. However, SFTP is faster and more efficient.