我需要将代码从 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/