如何在 Java 中通过 TLS/SSL (FTPS) 服务器连接到 FTP

How to connect to FTP over TLS/SSL (FTPS) server in Java

我无法通过 TLS/SSL (FTPS) 服务器连接到 FTP。我正在使用 SimpleFTP 库,我可以在没有 SSL 的情况下连接 FTP 服务器,但无法连接 FTPS.

它在第 2 行 (ftp.connect) 给我这个错误,

SimpleFTP received an unknown response when connecting to the FTP server:
220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------

我正在使用以下代码

SimpleFTP ftp = new SimpleFTP();

// Connect to an FTP server on port 21.
ftp.connect("xxx.xxx.xxx.xxx", 21, "username", "pwd");
//getting error at (ftp.connect) above line

// Set binary mode.
ftp.bin();

// Change to a new working directory on the FTP server.
ftp.cwd("web");
ftp.disconnect();

SimpleFTPclass/library根本不支持TLS/SSL


改用FTPSClient class from the Apache Commons Net library

查看 official example for the FTPClient class 并将 FTPClient 替换为 FTPSClient

FTPSClient ftpClient = new FTPSClient();
ftpClient.connect(host);
ftpClient.login(user, password);

FTPSClient class 默认为显式 TLS/SSL (推荐)。在极少数情况下,您需要隐式 TLS/SSL,使用 new FTPSClient(true).