Download file from FTP server?

I feel like I’m missing something simple here.

I just want to download and then display an image file from a remote, public FTP server. When I try to do so I get “The file xxx is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable. NullPointerException.”

Is this a server settings issue I cannot get around with Processing, or am I missing a step?

void setup(){
  size(400,400);

  PImage img = loadImage("ftp://public.sos.noaa.gov/rt/true_color/4096/TRUE.daily.20200713.color.png");
  image(img,400,400);

}

Thank you in advance!

Hi,

If you look at the source code for loadImage(), it looks like this (I deleted some lines for clarity):

// processing/core/src/processing/core/PApplet.java

  public PImage loadImage(String filename, String extension) { //, Object params) {

    // For jpeg, gif, and png, load them using createImage(),
    // because the javax.imageio code was found to be much slower.
    // http://dev.processing.org/bugs/show_bug.cgi?id=392
    try {
      if (extension.equals("jpg") || extension.equals("jpeg") ||
          extension.equals("gif") || extension.equals("png") ||
          extension.equals("unknown")) {
        byte[] bytes = loadBytes(filename);

        //....

It uses the loadBytes() method to load the image :

// processing/core/src/processing/core/PApplet.java

  public byte[] loadBytes(String filename) {
    String lower = filename.toLowerCase();
    // If it's not a .gz file, then we might be able to uncompress it into
    // a fixed-size buffer, which should help speed because we won't have to
    // reallocate and resize the target array each time it gets full.
    if (!lower.endsWith(".gz")) {
      // If this looks like a URL, try to load it that way. Use the fact that
      // URL connections may have a content length header to size the array.
      if (filename.contains(":")) {  // at least smells like URL
        InputStream input = null;
        try {
          URL url = new URL(filename);
          URLConnection conn = url.openConnection();
          int length = -1;

          if (conn instanceof HttpURLConnection) {
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            // Will not handle a protocol change (see below)
            httpConn.setInstanceFollowRedirects(true);
            int response = httpConn.getResponseCode();
            // Default won't follow HTTP -> HTTPS redirects for security reasons
            // http://stackoverflow.com/a/1884427
            if (response >= 300 && response < 400) {
              String newLocation = httpConn.getHeaderField("Location");
              return loadBytes(newLocation);
            }

      // .....

Then the loadBytes() function uses the HTTP protocol to load the image from a URL. So FTP doesn’t work.

It seems that ftp is not yet supported with Processing but looking at this thread :

https://forum.processing.org/beta/num_1131152633.html

you can use external libraries like edtFTPj or ftp4j.

1 Like

Oh interesting, thank you very much josephh. I will try those.

1 Like

Hrm, it might be best to just use something like winscp to schedule automatic synchronization so that I can just point my sketch to a local folder…

1 Like