How to upload a file to a FTP server?

I need to upload a text file to a FTP server, but don’t know how. I found this tutorial that uses the Commons net library, but since it was written in pure Java, processing gives me some error messages. The tutorial also says I have to add a file to the classpath. What does that mean? Anyways, here’s the code:



 * A program that demonstrates how to upload files from local computer
 * to a remote FTP server using Apache Commons Net API.
 * @author
public class FTPUploadFileDemo {

  public static void main(String[] args) {
    String server = "";
    int port = 21;
    String user = "user";
    String pass = "pass";

    FTPClient ftpClient = new FTPClient();
    try {

      ftpClient.connect(server, port);
      ftpClient.login(user, pass);


      // APPROACH #1: uploads first file using an InputStream
      File firstLocalFile = new File("D:/Test/");

      String firstRemoteFile = "";
      InputStream inputStream = new FileInputStream(firstLocalFile);

      System.out.println("Start uploading first file");
      boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
      if (done) {
        System.out.println("The first file is uploaded successfully.");

      // APPROACH #2: uploads second file using an OutputStream
      File secondLocalFile = new File("E:/Test/Report.doc");
      String secondRemoteFile = "test/Report.doc";
      inputStream = new FileInputStream(secondLocalFile);

      System.out.println("Start uploading second file");
      OutputStream outputStream = ftpClient.storeFileStream(secondRemoteFile);
          byte[] bytesIn = new byte[4096];
          int read = 0;

          while ((read = != -1) {
            outputStream.write(bytesIn, 0, read);

          boolean completed = ftpClient.completePendingCommand();
      if (completed) {
        System.out.println("The second file is uploaded successfully.");

    } catch (IOException ex) {
      System.out.println("Error: " + ex.getMessage());
    } finally {
      try {
        if (ftpClient.isConnected()) {
      } catch (IOException ex) {


So the errors:

No library found for
The package “org.apache.commons” does not exist. You might be missing a library.
*The method main cannot be declared static; static methods can only be declared in a
static or top level type 


There are some other minor issues, but I guess they’ll go away once the others are solved.
I have installed the library in the libraries folder of Processing.
I am thankful for any kind of answer!


If you want to do this in Processing, you will need to become familiar a bit with the Processing structure when it is written in java format right before building the code. Do this:

  • Run a simple example in Processing
  • In the PDE, go to the menu File >> Export. It will display a dialog box. Click export.
  • This action creates the transpiled version of your code. In other words, it will show the processing code converted to java. This is the code that you need to start with if you want to implement the code above.
  • Note: There are other ways to do this (!)

Ok, so now let’s go back. regarding the jar the mention in the tutorial, do you have it? If you don’t, then visit the provided link and download the jar. If you are on Windows, open a file explorer and navigate to where the jar is located. Open processing with your working sketch (important). Then, drag the jar file from the file browser and drop it into the PDE. This action moves/copies the jar file into your current sketch folder inside a folder called code. Then, when you execute your sketch file in the PDE, processing will make this jar available to your application.

You can find this link to do a better job in explaining what I just did.

Hope this helps,


1 Like

I don’t have enough time right now to get into that, but I’ll try when possible.
Also, for anyone in the future: There is an easier way to upload a file to an FTP server, which I found out about a few days ago. I’ll explain it in a sec. I got help from the forum of my website hoster:
So you could go to that forum, but I’ll explain it here as well:

  1. You create a text file, open it, and save it as a .bat file (make sure to choose all files). Voilà, step one complete.
  2. Now paste the following code into the file: (you can edit it by right clicking the file and then choosing “edit”)
    The lcd funcion uploads an entire folder, so make sure to create an extra folder for your file(s).
  3. In your Processing sketch, type: “exec(”\\fileDirectory//name")"
    There are a few things you need to know here, but first an example:
    exec(sketchPath() + "\\data//upload.bat");

And here’s the code for the .bat file:

@ftp -i -s:"%~f0"&GOTO:EOF
!:--- FTP commands below here ---
lcd C:/Your/path/here
cd  public_html/
mput "*.*"

There is a problem with this system though (I’ll explain how to solve it): the path in the .bat file doesn’t change when you move your sketchfolder. This means, that the file can’t find your upload-files anymore because they are in a different place. (I stumbled upon this after I exported the sketch --> the path changed, I could not upload anymore). The way I solved this was by creating a new .bat file when starting the sketch. You could do that by using saveStings(sketchPath() + "\data//upload.bat, CODE of the .bat file). Since the CODE contains a few quotation marks, you need to mark those with \". or else Processing won’t take them. That’s it!


Maybe shorter would be to use the free open-source, cross-platform curl comand line application.

That way you don’t even need to create a .bat file and you can execute the command directly from Processing.

1 Like

Be really careful about developing sketches that have your FTP passwords lying around in plain text folders. Don’t share them, don’t check the file into a public github repo, etc.

1 Like

Never thought about that! Thanks for the advice!

An alternative – if your sketch is realtime and has an “upload” button/UI – is to have the sketch ask for the password before it performs the upload. You can collect the password string with G4P or ControlP5 UI libraries, or use a simple javax.swing popup dialog.

Okay! I’ll look into that.

1 Like