I'm trying to connect to a FTPS Server using commons-net lib FTPSClient in a SpringBoot Application. I'm able to connect, login and change the directory using FTPSClient from commons-net lib. But as soon as I'm trying to list the file, it says "425 Unable to build data connection: TLS session of data connection not resumed." From what I know, the FTP Server were recently updated to only accept TLS Version 1.2 and above. I also tried to set the TLS Version, override "prepareDataSocket" method in FTPSClient, etc. But the same error "425 Unable to build data connection: TLS session of data connection not resumed." still throws. And I can list the files if I use the FileZilla application to access the FTPS Server, but not my Java code with commons-net library. I'm not sure why it happened.
The following is are code for connecting, login, and trying to list the files from FTPS Server.
System.setProperty("jdk.tls.client.protocols", "TLSv1.2,TLSv1.3");
FTPSClient ftp = new FTPSClient();
//Socket Opening TimeoutÏ
ftp.setDefaultTimeout(time10mins);
//Data Reading Timeout
ftp.setDataTimeout(time30mins);
//Connection Timeout
ftp.setConnectTimeout(time10mins);
String[] tlsVersions = {"TLSv1.2", "TLSv1.3"};
ftp.setEnabledProtocols(tlsVersions);
ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
ftp.connect(ftpProperties.getFtpHost(), ftpProperties.getFtpPort());
ftp.enterLocalPassiveMode();
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
throw new IOException("Exception in connecting to FTP Server");
}
//Socket Timeout after opening connection
ftp.setSoTimeout(time30mins);
ftp.login(ftpProperties.getFtpUsername(), ftpProperties.getFtpPassword());
ftp.execPROT("P");
ftp.execPBSZ(0);
ftp.type(FTP.BINARY_FILE_TYPE);
LOGGER.info("Connected to FTP location");
FileOutputStream fos;
LOGGER.info("Downloading files for " + today);
//Download Category Mapping files
ftp.changeWorkingDirectory(ftpProperties.getFtpDirectory());
FTPFile[] files = ftp.listFiles();
LOGGER.info("files length: " + files.length);
And the following are the logs that I can see:
`250 CWD command successful
SYST
215 UNIX emulated by FileZilla.
PASV
227 Entering Passive Mode
LIST
150 Starting data transfer.
425 Unable to build data connection: TLS session of data connection not resumed.`
As we can see, we can connection, login, and change the directory but LIST files failed. And I can even see "150 Starting data transfer" but still I get "TLS session of data connection not resumed." error. I also couldn't figure out why I can list the files if I use the FileZilla desktop to access the FTPS Server, but not the Java code. Thank you for your help!