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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36302985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:41:21  来源:igfitidea点击:

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

javasslftpftps

提问by Abdul Rafay

I'm stuck in connecting to FTP over TLS/SSL (FTPS) server. I am using SimpleFTP library through am able to connect FTP server without SSL but could not connect FTPS.

我一直在通过 TLS/SSL (FTPS) 服务器连接到 FTP。我正在使用 SimpleFTP 库,因为我能够在没有 SSL 的情况下连接 FTP 服务器,但无法连接 FTPS。

It is giving me this error at line 2 (ftp.connect),

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

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

SimpleFTP 在连接到 FTP 服务器时收到未知响应:
220--------- 欢迎使用 Pure-FTPd [privsep] [TLS] ----------

and am using below code

并且正在使用下面的代码

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();

采纳答案by Martin Prikryl

The SimpleFTPclass/library does not support TLS/SSL at all.

SimpleFTP班/库不支持TLS / SSL的。



Use the FTPSClientclassfrom the Apache Commons Net libraryinstead.

使用FTPSClientApache的共享网库代替。

See the official example for the FTPClientclassand just substitute the FTPClientwith the FTPSClient.

请参阅该类官方示例FTPClient,只需将 替换FTPClientFTPSClient.

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


The FTPSClientclass defaults to an explicit TLS/SSL (recommended). In a rare case you need an implicit TLS/SSL, use new FTPSClient(true).

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