Sample code for 30+ languages & platforms
Android™

Monitor FTP Transfer Progress

See more FTP Examples

Demonstrates the progress properties CurBytesReceived, CurBytesReceivedStr, CurBytesSent, CurBytesSentStr, ProgressMonSize, PercentDoneScale, and AutoGetSizeForProgress.

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: Percentage progress needs a known total, but FTP does not always report a file's size up front — so AutoGetSizeForProgress issues a SIZE command before a download, and ProgressMonSize supplies an expected size when even that is unavailable (a one-shot value consumed by the next transfer). PercentDoneScale sets the resolution of progress callbacks. The current byte counts are exposed both as 32-bit integers and as strings, the string forms avoiding integer-width limits for very large transfers.

Chilkat Android™ Downloads

Android™
// 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;

    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;
        }

    //  AutoGetSizeForProgress sends a SIZE command before a download so percentage-based progress can
    //  be computed.
    ftp.put_AutoGetSizeForProgress(true);

    //  PercentDoneScale sets the value representing one hundred percent in PercentDone callbacks.
    //  1000 gives tenth-of-a-percent resolution.
    ftp.put_PercentDoneScale(1000);

    //  ProgressMonSize is a one-shot expected size for the next download when the size is not
    //  otherwise known.  It is consumed by the next download and reset to 0.
    ftp.put_ProgressMonSize(20485760);

    success = ftp.GetFile("public_html/bigfile.zip","qa_output/bigfile.zip");
    if (success == false) {
        Log.i(TAG, ftp.lastErrorText());
        return;
        }

    //  After a transfer, the current byte counts are available as 32-bit numbers or as strings (the
    //  string forms avoid integer-size limits).
    Log.i(TAG, "Bytes received: " + String.valueOf(ftp.get_CurBytesReceived()));
    Log.i(TAG, "Bytes received (str): " + ftp.curBytesReceivedStr());
    Log.i(TAG, "Bytes sent: " + String.valueOf(ftp.get_CurBytesSent()));
    Log.i(TAG, "Bytes sent (str): " + ftp.curBytesSentStr());

    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."
  }
}