SFTP file Transfer using JSch

posted in: Tools | 0

JSch(Java Secure Channel) is a Java implementation of SSH2, which allows you to connect to a sshd server and perform various operations like File Transfer, Port forwarding, etc. JSch is distributed under BSD License and you could get the jar from their official website or SourceForge. JSch is also available in the Central Maven Repository.

Here’s how you could use JSch for file transfers on SSH2.

SSH file transfer using JSch

Below are the four steps that you could transfer files:

  1. Authentication
  2. Connection
  3. Set Path
  4. Transfer

1. Authentication:

We would be first creating a session by authenticating either using password or SSH keys. You could set other
required properties here and establish the session.

[java] JSch jsch = new JSch();
Session session = jsch.getSession(CredentialsManager.getUser(),
CredentialsManager.getHost(), CredentialsManager.getPort());
session.setPassword(CredentialsManager.getPassword());
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
[/java]

For SSH Key based authentication just use the following jsch.addIdentity(prvkey);

[java] JSch jsch = new JSch();
jsch.addIdentity("Key");
Session session = jsch.getSession(CredentialsManager.getUser(),
CredentialsManager.getHost(), CredentialsManager.getPort());
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
[/java]

2. Connection:

In this step, we require opening an SFTP connection to the server. Here we would be using channel as sftp, since we require for file transfer. Other possible option could be ‘shell’ if you want to connect to the shell.

[java] Channel channel = session.openChannel("sftp");
channel.connect();
[/java]

3. Set Path:

If you require changing the upload path to other than default

[java]channelSftp.cd(Path);[/java]

4. Transfer File using JSch SFTP:

Finally, transfer the data using put or get method.  You would require converting the upload data into to a stream first.

[java] channelSftp.put(stream, FileName);
[/java] [java] channelSftp.get(FileName, stream);
[/java]

Complete Code

In this case, I have used String to store the upload/download data. This could be changed to File or other Input/Output Streams according to your implementation.
File Upload
[java] public class FileUpload {
public void upload(String uploadData, String Path, String FileName) {
try {
JSch jsch = new JSch();
Session session = jsch.getSession("UserName",
"Host", "Post");
session.setPassword("Password");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp channelSftp = (ChannelSftp) channel;
channelSftp.cd(Path);
InputStream stream = new ByteArrayInputStream(
uploadData.getBytes(StandardCharsets.UTF_8));
channelSftp.put(stream, FileName);
channelSftp.exit();
session.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

[/java]
File Download
[java] public void FileDownload(String FileName) {
try {
JSch jsch = new JSch();
Session session = jsch.getSession(CredentialsManager.getUser(),
CredentialsManager.getHost(), CredentialsManager.getPort());
session.setPassword(CredentialsManager.getPassword());
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp channelSftp = (ChannelSftp) channel;
channelSftp.cd(Path);

ByteArrayOutputStream os = new ByteArrayOutputStream();
channelSftp.get(Filename, os);
BackupContents = os.toString();
channelSftp.exit();
session.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
}
[/java]

Leave a Reply

Your email address will not be published. Required fields are marked *