java - 用于 Java 的 Perl 的 Net::FTPSSL 模拟或如何在 Java 中使用 FTP over SSL

标签 java perl ssl ftp

我需要将代码从 Perl 迁移到 Java。

我在 perl 中有这段代码:

use Net::FTPSSL;

print "RUN\n";
my $ftpdebug    = 0;
my $ip          = "...";
my $port        = 2121;
my $atmpassword = "...";
my $atmuser     = "...";

my $ftp = Net::FTPSSL->new( $ip, Port => $port, Debug => $ftpdebug );

if ($ftp) {
    print "SUCCESS\n";
    $ftp->login( $atmuser, $atmpassword );
    print "LOGIN \n";
    @list = $ftp->list();
    for $i (@list) {
        print $i . "\n";
    }
} else {
    print "FAIL\n";
}

而且效果很好。它向我输出服务器上的文件列表。 但是当我尝试使用相同的主机和端口在 Java 中执行此操作时,我什至无法连接到服务器。 我正在尝试:

try {
    FTPSClient ftpClient = new FTPSClient(false);

    // Connect to host
    ftpClient.connect(host, 2121);
    int reply = ftpClient.getReplyCode();
    if (FTPReply.isPositiveCompletion(reply)) {

        // Login
        if (ftpClient.login(name, password)) {

            // Set protection buffer size
            ftpClient.execPBSZ(0);
            // Set data channel protection to private
            ftpClient.execPROT("P");
            // Enter local passive mode
            ftpClient.enterLocalPassiveMode();
            log.info(ftpClient.printWorkingDirectory());
            // Logout
            ftpClient.logout();

        } else {
            log.info("FTP login failed");
        }

        // Disconnect
        ftpClient.disconnect();

    } else {
        log.info("FTP connect to host failed: " + reply);
    }
} catch (IOException ioe) {
    log.info("FTP client received network error");
    log.info(ioe.getMessage());
    ioe.printStackTrace();
} catch (NoSuchAlgorithmException nsae) {
    log.info("FTP client could not use SSL algorithm");
}

它在这一行失败了:

ftpClient.connect(host, 2121);

错误是:

javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateExpiredException: NotAfter: Fri Dec 19 15:47:22 EET 2008

据我所知,FTPSClient 使用与 Net::FTPSSL 相同的协议(protocol) - FTP over SSL。那我做错了什么?

最佳答案

As I understand FTPSClient using the same protocol as Net::FTPSSL - FTP over SSL.

Net::FTPSSL 默认不验证证书(因此容易受到中间人攻击)。 FTPSClient 改为验证证书并发出声音,因为证书已在 2008 年过期:“CertificateExpiredException: NotAfter: Fri Dec 19 15:47:22 EET 2008”。

As I understand FTPSClient using the same protocol as Net::FTPSSL - FTP over SSL.

您尝试连接到具有无效(因为已过期)证书的站点。 FTPSClient 正确拒绝连接,而 Net::FTPSSL 盲目连接。

关于java - 用于 Java 的 Perl 的 Net::FTPSSL 模拟或如何在 Java 中使用 FTP over SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26255616/

相关文章:

iframe - HTTPS 页面托管 HTTP 子帧时的 HTTPS 混合内容提示

spring-boot - Spring Boot + Keycloak 使用私有(private)证书

java - Sametime 内的插件与 Lotus Notes Classess - 整个故事

java - 从 java 代码将 String 包装在 scala Future 中

regex - 如何在Perl正则表达式中匹配换行符\n?

perl - 如何使用 perl one-liner 作为别名

带有正则表达式匹配条件和赋值的 Perl grep 式单行?

asp.net - WCF 同步与异步

java - 小java程序打印不可见的换行符?

java - 在Docker中运行jar应用程序时出现重复图层