SFTP file Transfer using JSch

Jsch FTP

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.

Below are the four steps that you need to transfer files:

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

First we need to create a session with an authentication either by password or SSH keys.

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();

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

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();
No we need to open an SFTP connection to the server.

Channel channel = session.openChannel("sftp");
channel.connect();

If you want to change the upload path use,

channelSftp.cd(Path);

Finally, transfer the data using put or get method.  You need to convert the data into to a stream first.

channelSftp.put(stream, FileName);
channelSftp.get(FileName, stream);

Using above information the following code examples can be implemented for file upload and download purposes using sftp.

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();
        }
    }


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();
    }
}

Comments