Sample code for 30+ languages & platforms
Java

Execute an FTP Upload Plan

See more FTP Examples

Demonstrates the Chilkat Ftp2.PutPlan method, which executes a previously created upload plan. The first argument is the plan text and the second is the local path of a completion log that records each successful operation.

Note: The local paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: The completion log is what makes plan execution resumable: each successful upload is recorded, so if a run is interrupted, re-executing the same plan skips the operations already logged and continues where it left off — invaluable for a large transfer over a flaky connection. The plan's stored absolute remote paths are used regardless of the current remote directory, so execution is independent of session state.

Chilkat Java Downloads

Java
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    boolean success = false;

    //  Demonstrates the Ftp2.PutPlan method, which executes a previously created upload plan.  The 1st
    //  argument is the plan text and the 2nd is the local path of a completion log.

    CkFtp2 ftp = new CkFtp2();

    ftp.put_Hostname("ftp.example.com");
    ftp.put_Username("myFtpLogin");

    //  Normally you would not hard-code the password in source.  You should instead obtain it
    //  from an interactive prompt, environment variable, or a secrets vault.
    ftp.put_Password("myPassword");

    success = ftp.Connect();
    if (success == false) {
        System.out.println(ftp.lastErrorText());
        return;
        }

    success = ftp.ChangeRemoteDir("public_html");
    if (success == false) {
        System.out.println(ftp.lastErrorText());
        return;
        }

    //  Load a plan that was previously created with CreatePlan.
    CkStringBuilder sbPlan = new CkStringBuilder();
    success = sbPlan.LoadFile("qa_output/upload_plan.txt","utf-8");
    if (success == false) {
        System.out.println(sbPlan.lastErrorText());
        return;
        }

    String plan = sbPlan.getAsString();

    //  Execute the plan.  Each successful operation is recorded in the completion log, which lets an
    //  interrupted run be resumed without repeating completed uploads.  The plan uses its stored
    //  absolute remote paths even if the current remote directory has changed.
    success = ftp.PutPlan(plan,"qa_output/upload_done.log");
    if (success == false) {
        System.out.println(ftp.lastErrorText());
        return;
        }

    System.out.println("Upload plan executed.");

    success = ftp.Disconnect();
    if (success == false) {
        System.out.println(ftp.lastErrorText());
        return;
        }
  }
}