Sftp with Java / Processing

I know, I know, you’ve been waiting your whole life for this.

Download beta SFTP Processing library (source and example included in zip).

The library uses JSch (Java Secure Channel).
Copyright (c) 2002,2003,2004,2005,2006,2007 Atsuhiko Yamanaka, JCraft, Inc.

import sftp.*;
 
Sftp sftp;
 
void setup() {
  size(200,200);
  background(0);
  // Create the SFTP object
  // if 3rd arg = false, you must set the password in your code
  // if 3rd arg = true, you will be prompted to enter your password
  sftp = new Sftp("www.hostname.com","login", true);
  // sftp.setPassword("XXXXXX");
  sftp.start(); // start the thread
  noLoop();
}
 
void mousePressed() {
  // At any point you can execute an SFTP command 
  // Not all commands are currently implemented
  // but you do have "ls" and "get"
  // Gosh, I should implement "put", sorry!
  sftp.executeCommand("ls");
  sftp.executeCommand("get file.txt");
}
  • http://www.createdigitalmusic.com Peter Kirn

    Excellent … but now I think Amazon S3 support must be next. :) I’ll let you know how I fare. Everything’s documented for, like, real programmers with jobs and such, instead of … erm … me.

  • http://tiagopedras.com Tiago Pedras

    Hey, this is great! But what about that “put” function?
    Does it exist? Can I upload or does it only have download?

    Regards

  • http://www.shiffman.net Daniel

    No sorry, I never got around to it. If you look at the source though, you can see where to add it. I will try to take care of it soon though!

  • http://tiagopedras.com Tiago Pedras

    It’s too bad. =)
    I can’t seem to find a better alternative to use SFTP and yours seemed really nice. Couldn’t get it to work though due to the “libraries” folder miss location (can’t find where to put your files).

  • http://schmizz.net shikhar

    plug: give sshj (http://github.com/shikhar/sshj) a shot

  • Dennis Patel

    Hi I found a complete Java program to upload file to SFTP server at http://vigilance.co.in/java-program-for-uploading-file-to-sftp-server/

  • Dax Someone1001

    checkout apache camel if you are looking for ftp/sftp/ftps implementation in java

  • Tarun

    Okay for all who want the put command. Me and my friend did the put command for a project we were doing. Here is how to do it:

    1. Open the src/ftp folder in the zip file you downloaded from here and open the Sftp.java file. 
    2. After line 134 put the following code in :
    if(cmds[0].equals(“put”)) {            String p1=cmds[1];            String p2=”.”;            if(cmds.length==3)p2=cmds[2];            SftpProgressMonitor monitor=new Progress();            int mode=ChannelSftp.OVERWRITE;            try {                //sftp.get(p1, p2, monitor, mode); sftp.cd(p2); File f = new File(p1); sftp.put(new FileInputStream(f), f.getName()); } catch (SftpException e) {                e.printStackTrace();            }            return;        }

    Now save this file and recompile all the 3 files in this ftp folder to make the new sftp.jar file. Use that jar file instead of the original and the put command should start working.

    Special thanks to Kartik Agarwal for his help on this !