Android™
Android™
Create an FTP Upload Plan
See more FTP Examples
Demonstrates the Chilkat Ftp2.CreatePlan method, which creates and returns a textual upload plan for a local directory. The only argument is the local directory. The object must be connected, because the plan records the current absolute remote directory together with the local source paths.
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: A plan is a saved, resumable description of an upload — the list of local files and their absolute remote destinations — computed once and executed later (or on another run) with
PutPlan. Separating planning from execution suits large or unreliable transfers: the plan can be reviewed, stored, and re-run, and because it records absolute remote paths it stays correct even if the working directory later changes.Chilkat Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean success = false;
// Demonstrates the Ftp2.CreatePlan method, which creates and returns a textual upload plan for a
// local directory. The only argument is the local directory. The object must be connected,
// because the plan records the current absolute remote directory.
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) {
Log.i(TAG, ftp.lastErrorText());
return;
}
success = ftp.ChangeRemoteDir("public_html");
if (success == false) {
Log.i(TAG, ftp.lastErrorText());
return;
}
// The plan captures which local files to upload and the absolute remote destination. Save it so
// the upload can be run (or resumed) later with PutPlan.
String plan = ftp.createPlan("qa_data/site");
if (ftp.get_LastMethodSuccess() == false) {
Log.i(TAG, ftp.lastErrorText());
return;
}
// Persist the plan text for later use.
CkStringBuilder sbPlan = new CkStringBuilder();
sbPlan.Append(plan);
success = sbPlan.WriteFile("qa_output/upload_plan.txt");
if (success == false) {
Log.i(TAG, sbPlan.lastErrorText());
return;
}
Log.i(TAG, "Upload plan created.");
success = ftp.Disconnect();
if (success == false) {
Log.i(TAG, ftp.lastErrorText());
return;
}
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}