我正在尝试使用 Apache Commons Net 库连接到 FTP 服务器,其中初始连接是纯文本(和文件列表),但授权和数据传输是 SSL。我已经使用 CoreFTP 验证这是服务器的实际行为。我如何使用 Apache Commons 库完成此操作。
如果我使用普通的 FTPClient,我可以获得连接,但随后我收到此消息:503 USER:服务器策略要求所有客户端都受到保护。
如果我以这种方式尝试 FTPSClient
FTPSClient l_ftp = new FTPSClient("SSL", true); l_ftp.setAuthValue("SSL"); l_ftp.connect(l_host, l_port);
我收到此错误:javax.net.ssl.SSLException:无法识别的 SSL 消息,纯文本连接?
这有点道理,因为服务器需要纯文本连接而客户端正在尝试 SSL。
如果我试试这个
FTPSClient l_ftp = new FTPSClient("SSL", false); l_ftp.setAuthValue("SSL"); l_ftp.connect(l_host, l_port);
我明白了:
javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake Caused by: java.io.EOFException: SSL peer shut down incorrectly
我认为这可能意味着大致相同,服务器需要纯文本,客户端需要 SSL。
Apache Commons 库甚至可以做到这一点吗?
这是 CoreFTP 日志
Welcome to Core FTP, release ver 2.2, build 1857 (x64) -- © 2003-2014 WinSock 2.0 Mem -- 2,096,632 KB, Virt -- 8,589,934,464 KB Started on Monday October 26, 2015 at 14:18:PM Resolving nnnnnnn.nnnnn.com... Connect socket #900 to 222.222.222.222, port 21... 220 CONNECT:Enterprise Gateway 2.0.02. S48 FTP Server ready... 15:18:25 10-26-2015 AUTH SSL 234 AUTH: command accepted. Securing command channel ... TLSv1, cipher TLSv1/SSLv3 (RC4-MD5) - 128 bit USER omitted 331 Password required for omitted. PASS ********** 230 User omitted logged in. Session Id: 25846. PBSZ 0 200 PBSZ command accepted. PROT C 534 PROT Request denied for policy reasons. PROT cmd failed... CCC 200 CCC command channel is no longer secured. SYST 502 Command not implemented. Keep alive off... PWD 257 "omitted" is the current working Mailbox ID. PASV 227 PASV Entering passive mode (209,95,224,76,121,95). LIST Connect socket #940 to 209.95.224.76, port 31071... 150 Opening data connection. 226 Transfer complete. 0 Bytes sent. Transferred 0 bytes in 0.008 seconds
最佳答案
事实证明这是我运行的应用程序服务器中的某种库或其他冲突。当我将测试代码拉出到一个独立项目时,它运行良好。为了后代,这里是工作代码。
FTPSClient l_ftp = new FTPSClient("SSL", false); l_ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); l_ftp.setAuthValue("SSL"); l_ftp.connect(l_host, l_port); if (!l_ftp.login(l_username, l_password)) { // BAD! } l_ftp.execPBSZ(0L); l_ftp.execCCC(); l_ftp.pwd(); // DO STUFF l_ftp.logout(); l_ftp.disconnect();
关于java - apache commons ftp 连接纯文本但授权 SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33353364/