Sample code for 30+ languages & platforms
Java

FTPS with Mutual TLS Authentication (TLS Client Certificate)

See more FTP Examples

Demonstrates how to do mutual TLS authentication (using a client certificate from a .pfx/.p12).

Chilkat Java Downloads

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

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.

        Ftp2 ftp = new Ftp2();

        ftp.setHostname("ftp.example.com");
        ftp.setPort(21);

        // If using implicit TLS, you probably want port 990..
        ftp.setPort(990);

        // Set this to false for implicit TLS, otherwise set to true for explicit TLS (where port is typically 21).
        ftp.setAuthTls(false);

        // Set this to true for implicit TLS, otherwise set to false.
        ftp.setSsl(true);

        Cert cert = new Cert();
        try {
            cert.loadPfxFile("qa_data/pfx/example.pfx", "pfx_password");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Use this certificate for our TLS mutually authenticated connection:
        try {
            ftp.setSslClientCert(cert);
        } catch (ChilkatException e) {
            System.out.println(cert.getLastErrorText());
            return;
        }

        // Establish the TLS connection with the FTP server.
        try {
            ftp.connectOnly();

            // If a login is required, then login with the FTP account login/password.
            ftp.setUsername("myLogin");
            ftp.setPassword("myPassword");
            ftp.loginAfterConnectOnly();
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Do whatever you're doing to do ...
        // upload files, download files, etc...

        // .....
        // .....

        ftp.disconnect();
    }
}