27

I'm looking for a dead simple Java Library to use for SFTP file transfers. I don't need any other features beyond that.

I've tried Zehon's, but it's incredible naggy, and I think 8 jar files is a bit crazy for so little functionality as I require.

And the library have to be free (as in free beer), and preferable Open Source (not a requirement).

Thanks.

Stéphane Millien
  • 3,238
  • 22
  • 36
Claus Jørgensen
  • 25,882
  • 9
  • 87
  • 150

4 Answers4

46

Edit : I'm going to keep my previous answer, as JSch is still used in many places, but if you need a better-documented library, you can use sshj. An example in how to use it to do sftp is :

SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("host");
try {
    ssh.authPassword("username", "password");
    SFTPClient sftp = ssh.newSFTPClient();
    try {
        sftp.put(new FileSystemFile("/path/of/local/file"), "/path/of/ftp/file");
    } finally {
        sftp.close();
    }
} finally {
    ssh.disconnect();
}

Using JSch (a java ssh lib, used by Ant for example), you could do something like that :

Session session = null;
Channel channel = null;
try {
    JSch ssh = new JSch();
    ssh.setKnownHosts("/path/of/known_hosts/file");
    session = ssh.getSession("username", "host", 22);
    session.setPassword("password");
    session.connect();
    channel = session.openChannel("sftp");
    channel.connect();
    ChannelSftp sftp = (ChannelSftp) channel;
    sftp.put("/path/of/local/file", "/path/of/ftp/file");
} catch (JSchException e) {
    e.printStackTrace();
} catch (SftpException e) {
    e.printStackTrace();
} finally {
    if (channel != null) {
        channel.disconnect();
    }
    if (session != null) {
        session.disconnect();
    }
}

You can use JSch directly this way, or through Commons VFS, but then you'll have to have both commons vfs jar and jsch jar.

jediz
  • 4,459
  • 5
  • 36
  • 41
Valentin Rocher
  • 11,667
  • 45
  • 59
  • I found a simpler way to do it, I'll edit my answer. – Valentin Rocher Feb 27 '10 at 16:20
  • 1
    JSch is a great library but is severely lacking in documentation. I found http://timarcher.com/node/57 was a really helpful example. – UmYeah Feb 27 '10 at 16:55
  • Thanks for the link, I'll add it to the answer. Indeed, JSch really lacks documentation (and source code readability, it's like they released it in the wild), but it does the job perfectly. – Valentin Rocher Feb 27 '10 at 16:58
  • And the javadoc: http://mirrors.ibiblio.org/maven2/com/jcraft/jsch/0.1.50/jsch-0.1.50-javadoc.jar – Zon Jul 31 '13 at 18:58
  • 10
    JSch is a horrible library. No documentation. Horrible coding syntax should you try and figure it out via the source (they don't know what indentation is...). Uses obsoleted Java classes. Exceptions are all custom, but all generic (it uses the same custom exception class everywhere, and most of the time it is an empty message). Why do so many people recommend this disgusting thing? – Tustin2121 Jun 03 '16 at 20:32
  • Because I think it's the only one available :/ I'll be happy to update if you know another lib for SSH – Valentin Rocher Jun 03 '16 at 20:34
  • 2
    sshj seems to be much easier to use, so I've added it to the answer – Valentin Rocher Oct 28 '16 at 07:53
  • You could have use the more modern try-with-resources Java coding style. – L. G. Mar 27 '20 at 13:46
  • I don't agree with Tustin2121. I tried SSHJ with java 6 and 8; I encountered multiple crazy slowdown errors related its "bounty-castle" dependency (virtualbox system lacking entropy signed jar checking) : initialization can take minutes ! Bouncy-castle adds a packaging problem (no more fatjar!). Also, SSHJ exceptions are not always that clear... I ended up removing it from the project, I tried JSch and it's fine, exception messages are clean (though not typed). – Kiruahxh Sep 17 '20 at 19:37
  • @L.G. I could, but the answer dates from 2010 then 2016, and this would not add much to the answer – Valentin Rocher Sep 22 '20 at 08:34
10

Here is the complete source code of an example using JSch without having to worry about the ssh key checking.

import com.jcraft.jsch.*;

public class TestJSch {
    public static void main(String args[]) {
        JSch jsch = new JSch();
        Session session = null;
        try {
            session = jsch.getSession("username", "127.0.0.1", 22);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword("password");
            session.connect();

            Channel channel = session.openChannel("sftp");
            channel.connect();
            ChannelSftp sftpChannel = (ChannelSftp) channel;
            sftpChannel.get("remotefile.txt", "localfile.txt");
            sftpChannel.exit();
            session.disconnect();
        } catch (JSchException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (SftpException e) {
            e.printStackTrace();
        }
    }
}
Iraklis
  • 2,762
  • 1
  • 24
  • 31
  • 6
    This answer does not worry about security, not about key checking. By using `StrictHostKeyChecking=no` you lose any security. Do not recommend anyone to use it, without explaining the consequences. – Martin Prikryl Feb 16 '16 at 13:28
4

The exit() and the disconnect() must be placed in a finally block. In this example we've got a never ending program in case of an exception because a second thread won't terminate.

tg1
  • 49
  • 1
-5

How about FTPSClient from Apache Commons?

Valentin Rocher
  • 11,667
  • 45
  • 59
Konrad Garus
  • 53,145
  • 43
  • 157
  • 230