DEV Community

Teddy Zugana
Teddy Zugana

Posted on

Java FTPS disabling Certificate Check , FOR FIX FTP SSL certificate expired exception

1)the encryption of the FTPS server will be either TLS explicit encryption or TLS explicit encryption.Use the constructor arg for FTPS accordingly. like

FTPSClient ftpClient = new FTPSClient(false);
Enter fullscreen mode Exit fullscreen mode

2)if your FTPS server security certificate has expired, disable the check from client by

ftpClient.setTrustManager(new X509TrustManager() {
    @Override
    public X509Certificate[] getAcceptedIssuers() {
        return null;
    }

    @Override
    public void checkClientTrusted(X509Certificate[] certs, String authType) {
    }

    @Override
    public void checkServerTrusted(X509Certificate[] certs, String authType) {
    }
});
Enter fullscreen mode Exit fullscreen mode

3)enable file transfer between server and client using this method

ftpClient.enterLocalPassiveMode();
Enter fullscreen mode Exit fullscreen mode

4)use the right port number. Usually for explicit encryption it is 21 and for implicit it is 990

The above four are the common configs required to establish a connection. The end snippet looks something like this


FTPSClient ftpClient = new FTPSClient(false);
        ftpClient.setTrustManager(new X509TrustManager() {
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            @Override
            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        });
        ftpClient.connect("ftps.mydomain.com",21);
        boolean res = ftpClient.login("username", "password");
        if(!res) throw new Exception("unable to connect to ftps");
        int reply=ftpClient.getReplyCode();
        if(FTPReply.isPositiveCompletion(reply)){
            ftpClient.enterLocalPassiveMode();
            FTPFile[] ftpFiles = ftpClient.listFiles("/folder/subfolder");
            System.out.println("complete "+reply+" "+ftpFiles.length);
            for(FTPFile x: ftpFiles){
                System.out.println(x.getName());
            }
            ftpClient.retrieveFile("/folder/subfolder/file.tsv",new FileOutputStream(new File("C:\\Users\\myname\\Desktop\\out.csv")));
        }else{
            throw new RuntimeException("unable to get valid reply from ftp server. Reply code is "+reply);
        }
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more