Sample code for 30+ languages & platforms
Java

FTP Upload / Download to StringBuilder

Demonstrate how to upload from a Chilkat StringBuilder object, and download into a StringBuilder object.

Chilkat Java Downloads

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

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

        StringBuilder sbA = new StringBuilder();
        sbA.loadFile("qa_data/hamlet.xml", "utf-8");

        // Upload the contents of sbA to the FTP server.
        boolean bIncludeBOM = false;
        String remoteFilename = "hamletFromSb.xml";
        try {
            ftp.putFileSb(sbA, "utf-8", bIncludeBOM, remoteFilename);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Download...
        StringBuilder sbB = new StringBuilder();
        try {
            ftp.getFileSb(remoteFilename, "utf-8", sbB);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Verify that sbA and sbB have the exact same contents.
        System.out.println("size of sbA: " + sbA.getLength());
        boolean bCaseSensitive = true;
        if (sbA.contentsEqualSb(sbB, bCaseSensitive)) {
            System.out.println("Contents are equal. Success.");
        } else {
            System.out.println("Contents are NOT equal.  Failed.");
        }

        ftp.disconnect();
    }
}