Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java SFTP Transfer Library [closed]

Tags:

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.

like image 268
Claus Jørgensen Avatar asked Feb 27 '10 09:02

Claus Jørgensen


People also ask

How do I use SFTP in Java?

You can use SFTP to connect via SSH to your Java application and then transfer files. With SFTP, you connect using SSH and then transfer files with SFTP. To do this, you can use the JSch (Java secure channel) library.

Does SFTP lock file while transfer?

To prevent users from modifying files while the SFTP server is transferring them, you can enable SFTP file locking. By default, SFTP file locking is disabled.


1 Answers

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.

like image 136
Valentin Rocher Avatar answered Oct 21 '22 14:10

Valentin Rocher