Java 我无法使用带有自定义SSLContext的Apache FTPSClient通过FTPS连接到我的FTP服务器-getting;无法识别的SSL消息,纯文本连接?“;

Java 我无法使用带有自定义SSLContext的Apache FTPSClient通过FTPS连接到我的FTP服务器-getting;无法识别的SSL消息,纯文本连接?“;,java,ftp,ftps,apache-commons-net,ftp-server,Java,Ftp,Ftps,Apache Commons Net,Ftp Server,我正在编写通过FTPS连接到FTP服务器的程序 源代码: String protocol = "TLS"; String host = "192.168.5.165"; String username = "usr"; String password = "111"; String trustStorePath = "C:/TEMP/truststore"; String trustStorePassword = "111111"; int port = 990; FTPSClient cli

我正在编写通过FTPS连接到FTP服务器的程序

源代码:

String protocol = "TLS";
String host = "192.168.5.165";
String username = "usr";
String password = "111";
String trustStorePath = "C:/TEMP/truststore";
String trustStorePassword = "111111";
int port = 990;

FTPSClient client = new FTPSClient(protocol);

KeyStore trustStore = loadStore("JKS", new File(trustStorePath), trustStorePassword);
X509TrustManager trustManager = TrustManagerUtils.getDefaultTrustManager(trustStore);

SSLContext ctx = SSLContext.getInstance("TLSv1.1");
ctx.init(null, new TrustManager[] {trustManager}, null);

FTPSSocketFactory socketFactory = new FTPSSocketFactory(ctx);
client.setSocketFactory(socketFactory);

client.addProtocolCommandListener(new PrintCommandListener(new 
PrintWriter(System.out)));
client.connect(host, port);
我在信任库中导入了服务器的证书。但在运行此代码后,我得到了以下错误:

220 Wing FTP服务器就绪。。。(未注册的WING FTP服务器)
认证TLS
234验证命令正常。正在初始化TLS连接。
javax.net.ssl.SSLException:无法识别的ssl消息,纯文本连接?
位于sun.security.ssl.InputRecord.handleUnknownRecord(InputRecord.java:710)
位于sun.security.ssl.InputRecord.read(InputRecord.java:527)
位于sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973)
位于sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
位于sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
位于sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
在org.apache.commons.net.ftp.FTPSClient.sslnegationation上(FTPSClient.java:269)
在org.apache.commons.net.ftp.FTPSClient.\u connectAction.\u(FTPSClient.java:211)
位于org.apache.commons.net.SocketClient.connect(SocketClient.java:183)
位于org.apache.commons.net.SocketClient.connect(SocketClient.java:203)
在edtestb.remsenergy.FTPSTest.test(FTPSTest.java:63)

我做错了什么?

在我看来,你设计得太过火了

我不确定我是否能完全理解您的代码的作用,但我相信您无意中对连接进行了双重加密


如果您真的想连接到隐式FTPS端口990,只需使用

FTPSClient client = new FTPSClient(protocol, true);
或者,由于默认设置为
protocol=“TLS”
,这也可以:

FTPSClient client = new FTPSClient(true);

client.connect(host); // 990 is default, with FTPSClient with isImplicit=true

虽然隐式FTPS已经过时,但通过连接到默认的FTP端口21,您最好使用显式FTPS(如果您的服务器支持,大多数都支持):

FTPSClient client = new FTPSClient();

client.connect(host);

如果需要自定义的
SSLContext
,只需使用相应的
FTPSClient
重载,如:

FTPSClient client = new FTPSClient(ctx);

client.connect(host);

这看起来很奇怪。您正在使用显式FTPS协议连接到隐式FTPS端口990。你能用同样的方法连接任何独立的FTP客户端吗?向我们显示客户端的详细日志文件。即使我通过隐式模式(FTPSClient(protocol,true))连接,我也会收到相同的错误。您是否删除了
setSocketFactory
?非常感谢您的回复<代码>代码FTPSClient客户端=新的FTPSClient();addProtocolCommandListener(新的PrintCommandListener(新的PrintWriter(System.out)));client.connect(主机);client.login(用户名、密码);客户。执行保护(“P”);FTPFile[]files=client.listDirectories();对于(FTPFile file:files){System.out.println(file.getName());}非常感谢您的回复。是的,我的服务器支持显式FTP,我需要通过显式模式连接到服务器。我照你说的做了<代码>FTPSClient客户端=新的FTPSClient();addProtocolCommandListener(新的PrintCommandListener(新的PrintWriter(System.out)));client.connect(主机);client.login(用户名、密码);客户。执行保护(“P”);FTPFile[]files=client.listDirectories();对于(FTPFile file:files){System.out.println(file.getName());}我收到了以下日志:
220 FileZilla服务器0.9.60 beta 220请访问https://filezilla-project.org/ 使用身份验证类型TLS usr 331对TLS 234进行身份验证usr PASS 111 230登录保护P 200保护级别设置为P SYST 215 UNIX所需的密码由FileZilla PORT 192168,5165199,26 200 PORT命令模拟成功列表150为目录列表打开数据通道“/”已从目标VM断开连接,地址:'127.0.0.1:50964',传输:'socket'javax.net.ssl.SSLHandshakeException:握手期间远程主机关闭连接
您现在遇到了另一个问题。很可能是这个: