The AHO students needed a simple file uploader that would automatically transfer files from a local folder to a web server. The following application will simply watch a given folder and upload any files it contains to the FTP server. Note that it will delete the local copy upon successful upload, so be careful how you use it.

Code: FTPUploader.pde

The code for the application is given below, but downloading the following ZIP will give you the required edtFTPj library files as well as a sample config file:

// FTPUploader.pde - watches a given folder for files to upload
// to a FTP server. Warning: Deletes the local copy on successful transfer.
//
// This code uses the excellent edtFTPj Open Source library:
// http://www.enterprisedt.com/products/edtftpj/
//
// Copy the jar files from the "lib" folder of edtFTPj to the "code"
// folder of your sketch.
//
// Marius Watz - http://workshop.evolutionzone.com/

import com.enterprisedt.net.ftp.*;
import java.util.Properties;
import java.io.*;

Properties conf;
public String host,user,pass,localdir,remotedir;
FTPClient ftp;

void setup() {
  size(100,100);
  try {
    conf=new Properties();
    conf.load(getFileStream(sketchPath("FTP.conf")));
    host=conf.getProperty("host");
    user=conf.getProperty("user");
    pass=conf.getProperty("pass");
    localdir=conf.getProperty("localdir");
    remotedir=conf.getProperty("remotedir");

    println("FTPUploader\n\nHost: "+host+
      "\nLocal dir: "+localdir+
      "\nRemote dir: "+remotedir);
  }
  catch (Exception e) {
    die("Problem reading FTP.conf: "+e.toString());
  }
}

public FileInputStream getFileStream(String filename) {
  try {
    File f=new File(filename);
    f=f.getCanonicalFile();
    FileInputStream in=new FileInputStream(f);
    return in;
  }
  catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return null;
  }
}

// checks for files in a given directory. all files placed
// in this directory will be uploaded and then deleted
public void checkDir() {
  try {
    File dir=new File(localdir);
    String [] filenames=dir.list();

    if(filenames!=null && filenames.length>0) {
      ftp=new FTPClient(host);
      ftp.login(user, pass);
      ftp.setConnectMode(FTPConnectMode.PASV);
      ftp.setType(FTPTransferType.BINARY);
      ftp.chdir(remotedir);

      for(int i=0; i< filenames.length; i++) {
        println("uploading "+filenames[i]);
        ftp.put(localdir+"/"+filenames[i],filenames[i]);
        println(ftp.getLastReply().getReplyCode()+" "+ftp.getLastReply().getReplyText());

        // if successful then delete the local copy
        if(Integer.parseInt(ftp.getLastReply().getReplyCode())==226) {
          File del=new File(localdir+"/"+filenames[i]);
          println("deleting "+filenames[i]+": "+del.delete());
        }
      }

/*
      println ("Directory after put");
      String [] files = ftp.dir(".", true);
      println(files);
      */

      ftp.quit();
    }
  }
  catch (Exception e) {
    println("Problem uploading: "+e.toString());
    try {
      if(ftp!=null) ftp.quitImmediately();
    }
    catch (Exception e1) {
      e1.printStackTrace();
    }
  }
}

public void draw() {
  checkDir();
  delay(5000);
}

public void stop() {
  try {
    if(ftp!=null) ftp.quitImmediately();
  }
  catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }

  super.stop();
}

2 Comments »

There are 2 comments to "Code: FTPUploader.pde". You may leave your own comment.
1. Knut, December 16th, 2007 at 15:29

Hi, the code worked well, but we never got ftp access from our school. So i had to write code using JScript/Windows Scripting Host to convert the images into jpg from tga, and move images to a mapped network drive instead. Wich sort of worked. I guess it could have been done in processing, but the image converting and scaling took less resources when using WSH and Imagemagick.

2. uuganbayar, April 9th, 2009 at 18:54

thanks, this code is working well .

Comment on this entry

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">