Java: What is the best way to SFTP a file from a server?

Another option is to consider looking at the JSch library. JSch seems to be the preferred library for a few large open source projects, including Eclipse, Ant and Apache Commons HttpClient, amongst others. It supports both user/pass and certificate-based logins nicely, as well as all a whole host of other yummy SSH2 features.

Here's a simple remote file retrieve over SFTP. Error handling is left as an exercise for the reader :-) JSch jsch = new JSch(); String knownHostsFilename = "/home/username/. Ssh/known_hosts"; jsch.

SetKnownHosts( knownHostsFilename ); Session session = jsch. GetSession( "remote-username", "remote-host" ); { // "interactive" version // can selectively update specified known_hosts file // need to implement UserInfo interface // MyUserInfo is a swing implementation provided in // examples/Sftp. Java in the JSch dist UserInfo ui = new MyUserInfo(); session.

SetUserInfo(ui); // OR non-interactive version. Relies in host key being in known-hosts file session. SetPassword( "remote-password" ); } session.connect(); Channel channel = session.

OpenChannel( "sftp" ); channel.connect(); ChannelSftp sftpChannel = (ChannelSftp) channel; sftpChannel. Get("remote-file", "local-file" ); // OR InputStream in = sftpChannel. Get( "remote-file" ); // process inputstream as needed sftpChannel.exit(); session.disconnect().

1 Cheekysoft, I noticed - while using Jsch - removing files on the sftp-server does not work. Also renaming files does not work too. Any ideas please?

Andy – andy Jan 31 at 1:24 1 Sorry, it is not something I work with at the moment. (Please try and leave these sort of responses as comments -- like this message -- and not as a new answer to the original question) – Cheekysoft Jan 31 at 1:24.

This was the solution I came up with sourceforge.net/projects/sshtools/ (most error handling omitted for clarity). This is an excerpt from my blog SshClient ssh = new SshClient(); ssh. Connect(host, port); //Authenticate PasswordAuthenticationClient passwordAuthenticationClient = new PasswordAuthenticationClient(); passwordAuthenticationClient.

SetUsername(userName); passwordAuthenticationClient. SetPassword(password); int result = ssh. Authenticate(passwordAuthenticationClient); if(result!

= AuthenticationProtocolState. COMPLETE){ throw new SFTPException("Login to " + host + ":" + port + " " + userName + "/" + password + " failed"); } //Open the SFTP channel SftpClient client = ssh.openSftpClient(); //Send the file client. Put(filePath); //disconnect client.quit(); ssh.disconnect().

J2ssh is pretty buggy – Bruce Blackshaw Feb 13 '10 at 5:13 1 I agree (belatedly), it worked fine for the original site/download I required but it refuesed to work for the new one. I'm in the process of switching to JSch – David Hayes Jan 27 at 14:09.

A nice abstraction on top of Jsch is Apache commons-vfs which offers a virtual filesystem API that makes accessing and writing SFTP files almost transparent. Worked well for us.

– bene May 15 '09 at 13:46 Yes it is. If you need non-standard identities you can call SftpFileSystemConfigBuilder.getInstance(). SetIdentities(...).

– Russ Hayward Jan 27 at 11:05.

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

Below is an example using Apache Common VFS: FileSystemOptions fsOptions = new FileSystemOptions(); SftpFileSystemConfigBuilder.getInstance(). SetStrictHostKeyChecking(fsOptions, "no"); FileSystemManager fsManager = VFS.getManager(); String uri = "sftp://user:password@host:port/absolute-path"; FileObject fo = fsManager. ResolveFile(uri, fsOptions).

I use this SFTP API called Zehon, it's great, so easy to use with a lot of sample code. Here is the site zehon.com.

I found complete working example for SFTP in java using JSCH API vigilance.co.in/java-program-for-uploadi....

Andy , to delete file on remote system,you need to use (channelExec) of JSch and pass unix/linux commands to delete it.

The best solution I've found is Paramiko. There's a Java version.

You also have JFileUpload with SFTP add-on (Java too): jfileupload.com/products/sftp/index.html.

Try edtFTPj/PRO, a mature, robust SFTP client library that supports connection pools and asynchronous operations. Also supports FTP and FTPS so all bases for secure file transfer are covered.

Sshj has a complete implementation of SFTP version 3 (what OpenSSH implements).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions


Thank You!
send