Sample code for 30+ languages & platforms
Java

FTP Upload / Download to a BinData Object

Demonstrates how to FTP upload the contents of a BinData object, and FTP downloads to the a BinData object.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Ftp2;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example assumes Chilkat Ftp2 to have been previously unlocked.
        // See Unlock Ftp2 for sample code.

        Ftp2 ftp = new Ftp2();

        ftp.setHostname("www.my-ftp-server.com");
        ftp.setUsername("mFtpLogin");
        ftp.setPassword("myFtpPassword");

        // Connect to the FTP server.
        try {
            ftp.connectOnly();

            // Authenticate with the FTP server.
            ftp.loginAfterConnectOnly();
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        BinData bdA = new BinData();
        bdA.loadFile("qa_data/jpg/penguins.jpg");

        // Upload the contents of bdA to the FTP server.
        String remoteFilename = "penguins.jpg";
        try {
            ftp.putFileBd(bdA, remoteFilename);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Download...
        BinData bdB = new BinData();
        try {
            ftp.getFileBd(remoteFilename, bdB);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Verify that bdA and bdB have the exact same contents.
        System.out.println("size of bdA: " + bdA.getNumBytes());
        if (bdA.contentsEqual(bdB)) {
            System.out.println("Contents are equal. Success.");
        } else {
            System.out.println("Contents are NOT equal.  Failed.");
        }

        ftp.disconnect();
    }
}