Sample code for 30+ languages & platforms
Java

SFTP Upload and Download to a BinData Object

See more SFTP Examples

Demonstrates how to SFTP upload from a BinData object, and download into a BinData object.

Chilkat Java Downloads

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

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        SFtp sftp = new SFtp();

        // Set some timeouts, in milliseconds:
        sftp.setConnectTimeoutMs(5000);
        sftp.setIdleTimeoutMs(10000);

        // Connect to the SSH server and then authenticate.
        int port = 22;
        try {
            sftp.connect("MY-SSH-SERVER-DOMAIN-OR-IP", port);
            sftp.authenticatePw("MY-SSH-LOGIN", "MY-SSH-PASSWORD");
            sftp.initializeSftp();
        } 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 SSH/SFTP server.
        String remotePath = "sftpTesting/penguins.jpg";
        try {
            sftp.uploadBd(bdA, remotePath);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Download the file..
        BinData bdB = new BinData();
        try {
            sftp.downloadBd(remotePath, 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.");
        }

        sftp.disconnect();
    }
}