Java SFTP

Believe it or not, there are very few options currently when it comes to implementing SFTP in Java. I believe that the Apache Mina project will eventually provide support for it (http://mina.apache.org/sshd/index.html) however, the project is still at version 0.5 and I have not found any good documentation for it. jCraft’s JSch package is the most straightforward library that I have used so far and in this article, I am going to provide a sample implementation. 
JSch: http://www.jcraft.com/jsch/
JSch download: http://sourceforge.net/projects/jsch/files/jsch.jar/0.1.44/jsch-0.1.44.jar/download

Something to keep in mind here is that SFTP is not FTPS. SFTP is SSH FTP hence why I am hoping that Apache Mina will eventually support it. FTPS is FTP SSL which you can interact with using Apache FtpServer (http://mina.apache.org/ftpserver/). Until there is a mature Apache project, I recommend JSch. Actually, I recommend that you first try using SSH Fuse (http://fuse.sourceforge.net/sshfs.html) to mount your SFTP account on your file system and if you cannot set it up, then use JSch.
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpProgressMonitor; 
public class SFTPExample {
    public static void main(String args[]) throws Exception {
        String user = "enter user name";
        String password = "enter password";
        String host = "enter host";
        int port = 22;
        String knownHostsFilename = "/path/to/.ssh/known_hosts";        
        String sourcePath = "/path/to/test_file";
        String destPath = "test_file";        
        JSch jsch = new JSch();
        jsch.setKnownHosts(knownHostsFilename);
        Session session = jsch.getSession(user, host, port);
        session.setPassword(password);
        session.connect(); 
        ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
        sftpChannel.connect();
        System.out.println("Uploading test file");
        sftpChannel.put(sourcePath, destPath, new ExampleProgressMonitor());      
        System.out.println("Downloading test file");
        sftpChannel.get(destPath, sourcePath + ".new",  new ExampleProgressMonitor());        
        System.out.println("Remove test file");
        sftpChannel.rm(destPath);     
        sftpChannel.exit();
        session.disconnect();
    }

class ExampleProgressMonitor implements SftpProgressMonitor {
    private double count;
    private double max;
    private String src;
    private int percent;
    private int lastDisplayedPercent; 
    ExampleProgressMonitor() {
        count = 0;
        max = 0;
        percent = 0;
        lastDisplayedPercent = 0;
    }
    public void init(int op, String src, String dest, long max) {
        this.max = max;
        this.src = src;
        count = 0;
        percent = 0;
        lastDisplayedPercent = 0;
        status();
    }
    public boolean count(long count) {
        this.count += count;
        percent = (int) ((this.count / max) * 100.0);
        status();
        return true;
    } 
    public void end() {
        percent = (int) ((count / max) * 100.0);
        status();
    } 
    private void status() {
        if (lastDisplayedPercent <= percent - 10) {
            System.out.println(src + ": " + percent + "% " + ((long) count) + "/" + ((long) max));
            lastDisplayedPercent = percent;
        }
    }
}

  1. buy-steroids-uk--co reblogged this from verycrispy
  2. verycrispy posted this
Short URL for this post: http://tmblr.co/Zd8FQxE1CLSK