首页 文章

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

提问于
浏览
6

我搜索了SO,谷歌搜索超过2天,没有找到任何帮助我的答案 .

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

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

SimpleFTP在连接到FTP服务器时收到未知响应:220 ----------欢迎使用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();

1 回答

  • 18

    SimpleFTP 类/库根本不支持TLS / SSL .


    请改用Apache Commons Net library中的FTPSClient class .

    请参阅official example for the FTPClient class,然后将 FTPClient 替换为 FTPSClient .

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

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

相关问题