Updated February 18, 2023
Introduction to Java SFTP
The following article provides an outline for Java SFTP. We know that SFTP means SSH file transfer protocol used to transmit the data. Basically, SFTP first establishes the connection between the two different devices, after getting authentication or we can say that acknowledgment from the receiver, the sender can start sending the data. In other words, we can say that moving files over the network as well as different servers is a usual process, first, we have the FTP protocol and nowadays SFTP replaced the policy of FTP protocol.
Key Takeaways
- SFTP provides the functionality to send and receive text files over the digital platform.
- SFTP helps us to manage the secure file with help of PCI DSS and Encryption transfer over the network channel.
- Another more important point is that it compresses the data and provides authentication which means username and password during transmission.
What is Java SFTP?
Before we continue into Java, how about we rapidly characterize the distinctions between the famous moving components? Moving a document with Record Move Convention (FTP) or FTPS (FTP over SSL) isn’t equivalent to doing it with SFTP (Secure FTP or FTP over SSH).
While FTP and FTPS utilize similar FTP establishments with slight contrasts, SFTP is a totally unique convention. SFTP utilizes the solid shell convention (SSH) to lay out a channel and afterward communicate information. To confirm clients, SFTP can utilize a secret key or public-key verification. In addition, it can likewise scramble information correspondences among clients and servers.
We can utilize SFTP to associate through SSH to your Java application and afterward move records. With SFTP, you associate utilizing SSH and afterward move documents with SFTP.
How to Connect Java SFTP?
Here we will utilize a climate variable named SFTPTOGO_URL that contains all the data expected to interface with an SFTP server in a URI design: sftp: sftp://speciifeduser:password@host. The variable is parsed to remove the URI parts utilizing URI.parse, and the distant server’s host key is confirmed naturally utilizing ~/.ssh/known_hosts.
While launching the SftpClient class, pass the host, username, and alternatively the port (the default port number is 22). To verify with a secret password, utilize the authPassword capability. To verify utilizing public key verification, give the way to the confidential key and the pass string to the authKey capability.
How to Transfer File SFTP in Java?
First, we need to create the maven project and inside the pom.xml we need to add the jsch dependency as shown below.
Code:
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
In the next step, we need to set up the JSch with the help of password authentication or we can use the public key to access the remote server, sample code as shown below.
Code:
private sampleSftp setupJsch() throws JSchException {
JSch obj = new JSch();
obj.setKnownHosts("/Users/RRT/.ssh/known_hosts");
Session sobj = obj.getSession(user_name, remoteHost);
sobj.setPassword(pass);
sobj.connect();
return (sampleSftp) sobj.openChannel("sftp");
}
In the above code, we used remoteHost to represent the IP address of the server that we can also define with the help of a variable if we require it. If we want to generate the host key, then we can use the below command as follows.
ssh-keyscan –H –t rsa specified remote hostname
In the third step, we need to write the code for the upload file as shown below:
sampleSftp.put(localfile, remoteDir + "specified file name");
Here the specified file name means the actual file name which we need to upload on the server.’
Next, we need to write the code for the download file so we can use the below code.
sampleSftp.get(localfile, remoteDir + "specified file name");
In the same way, we need to add the maven dependency of SSHJ inside the pom.xml file as shown below.
Code:
<dependency>
<groupId>com.hierynomus</groupId>
<artifactId>sshj</artifactId>
<version>0.27.0</version>
</dependency>
If we want to add the updated version of sshj then we can use maven central, and the remaining steps are the same as we already discussed in the above point.
Example of Java SFTP
Given below is the example mentioned:
Code:
package com.sample;
import com.jcraft.jsch.*;
public class filetransfer {
private static final String h_ip = "0.0.0.0";
private static final String remote_user_id = "provide specified id of user";
private static final String remote_user_pass = " provide specified password of user ";
private static final int port_no = 22;
private static final int s_timeout = 80000;
private static final int c_timeout = 4000;
public static void main(String[] args) {
String l_file = "https://cdn.educba.com/rrt/demoproject/transfer/sample.txt";
String r_file = "https://cdn.educba.com/rrt/demoproject/received/sample1.txt ";
Session j_session = null;
try {
JSch jsch_obj = new JSch();
jsch_obj.setKnownHosts("/rrt/demoproject/.ssh/known_hosts");
j_session = jsch_obj.getSession(user_id, host_ip, port_no);
j_session.setPassword(pass_word);
// provides 20 seconds for the session timeout
j_session.connect(s_timeout);
Channel sftpObj = j_session.openChannel("sftp_obj");
// provides 10 seconds for the session timeout
Sftp_obj.connect(c_timeout);
ChannelSftp channelSftp_obj = (ChannelSftp) sftp;
// transferring file
channelSftp.put(l_file, r_file);
channelSftp_obj.exit();
}
} finally {
if (j_session != null) {
j_session.disconnect();
}
}
System.out.println("File transfer successfully done!!!!!!!!!!");
}
}
Explanation:
- In the above example, we just implemented code for file transfer. If you want to add a download file, then we can easily add it.
- The result of the above implementation is shown in the below screenshot.
Output:
FAQ
Given below are the FAQs mentioned:
Q1. Why do we use SFTP?
Answer: Basically, it provides a secure path to transmit and receive the file over the remote server. Suppose if we want to transfer the billing information, any type of fund, or we can say that data recovery then we can use SFTP.
Q2. Is there any difference between FTP and SFTP?
Answer: We know FTP is a traditional way to transfer files that we can share over the internet. SFTP provides security during the file transfer.
Q3. What are Push and Pull in SFTP?
Answer: When we transmit the file it is called a push file and when we download the file then we called pull operations
Conclusion
In this article, we have seen what Java SFTP is, as well as we saw some basic key ideas of Java SFTP with examples. We also saw the uses and features of the Java SFTP and how we can use them.
Recommended Articles
This is a guide to Java SFTP. Here we discuss the introduction, and how to connect Java SFTP. how to use transferring file SFTP in Java? and example. You may also have a look at the following articles to learn more –