简体   繁体   English

Java代码中的commons-net FTPSClient错误

[英]commons-net FTPSClient error in Java code

I am using commons-net package for the first time and using the code below to ftp a file from a FileZilla server over SSL. 我是第一次使用commons-net软件包,并使用下面的代码通过SSL从FileZilla服务器ftp文件。 I am facing an error when I run the program. 运行程序时遇到错误。 What is wrong in the code? 代码有什么问题?

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;

public class CommonsNetFTPSTest {
public static void main(String[] args) throws Exception {
    System.setProperty("javax.net.debug", "ssl");

    String server = "XXX.XXX.XXX.XXX";
    String username = "USER_TEST";
    String password = "ABCD1234";
    String remoteFile = "/Data/Input/PH240819";
    String localFile = "PH240819";
    String protocol = "SSL"; // TLS / null (SSL)
    int port = 990;
    int timeoutInMillis = 10000;
    boolean isImpicit = true;

    FTPSClient client = new FTPSClient(protocol, isImpicit);

    client.setDataTimeout(timeoutInMillis);
    client.addProtocolCommandListener(new PrintCommandListener(new  PrintWriter(System.out)));

    System.out.println("################ Connecting to Server ################################");

    try
    {
        int reply;
        System.out.println("################ Connect Call ################################");
        client.connect(server, port);

        client.login(username, password);

        System.out.println("################ Login Success ################################");

        //client.setFileType(FTP.BINARY_FILE_TYPE);
        client.setFileType(FTP.NON_PRINT_TEXT_FORMAT);
        client.execPBSZ(0);  // Set protection buffer size
        client.execPROT("P"); // Set data channel protection to private
        client.enterLocalPassiveMode();

        System.out.println("Connected to " + server + ".");
        reply = client.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply))
        {
            client.disconnect();
            System.err.println("FTP server refused connection.");
            System.exit(1);
        }

        client.listFiles();
        boolean retrieved = client.retrieveFile(remoteFile, new FileOutputStream(localFile));
    }
    catch (Exception e)
    {
        if (client.isConnected())
        {
            try
            {
                client.disconnect();
            }
            catch (IOException ex)
            {
                ex.printStackTrace();
            }
        }
        System.err.println("Could not connect to server.");
        e.printStackTrace();
        return;
    }
    finally
    {
        //client.disconnect();
        client.logout();
        System.out.println("# client disconnected");
    }
}
}

Error is below- 错误低于-

main, WRITE: SSLv3 Handshake, length = 56
main, READ: SSLv3 Change Cipher Spec, length = 1
JsseJCE:  Using cipher RC4 from provider TBD via init
CipherBox:  Using cipher RC4 from provider from init IBMJCE version 1.2
JsseJCE:  Using MessageDigest MD5 from provider IBMJCE version 1.2
main, READ: SSLv3 Handshake, length = 56
*** Finished
verify_data:  { 7, 71, 60, 4, 21, 222, 78, 66, 166, 137, 172, 57, 64, 131, 115, 89, 94, 128, 164, 80, 172, 124, 246, 14, 224, 91, 88, 128, 21, 44, 149, 161, 130, 112, 250, 11 }
***
cached session [Session-1, SSL_RSA_WITH_RC4_128_MD5]
%% Cached client session: [Session-1, SSL_RSA_WITH_RC4_128_MD5]
Exception in thread "main" java.lang.NullPointerException
    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:441)
    at org.apache.commons.net.ftp.FTPSClient.sendCommand(FTPSClient.java:535)
    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:520)
    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:569)
    at org.apache.commons.net.ftp.FTP.quit(FTP.java:781)
    at org.apache.commons.net.ftp.FTPClient.logout(FTPClient.java:706)
    at CommonsNetFTPSTest.main(CommonsNetFTPSTest.java:85)

Looks like it is not going beyond the point where the login is called. 看起来并没有超出登录名的范围。 The user id and password is all correct. 用户名和密码均正确。 I have an SSL Certificate which was given to me by the folks hosting the FileZilla server and after extracting the certificate to certfile.txt file using openSSL, I have installed it in my java/jre/lib/security/cacerts using command below- 我有一个由FileZilla服务器托管人员提供的SSL证书,并使用openSSL将证书提取到certfile.txt文件后,已使用以下命令将其安装在我的java / jre / lib / security / cacerts中:

keytool -import -alias "cert" -file certfile.txt -keystore /usr/java5/jre/lib/security/cacerts

I am still not able to determine the cause of the error. 我仍然无法确定错误原因。 as per the execution log prior to the error text , it looks like it successfully connects and handshakes, but something goes wrong right after that. 根据错误文本之前的执行日志,看起来它已成功连接并握手,但此后出现了问题。 I am running the program from a Unix box (AIX 5.3), which is the client machine. 我正在从作为客户机的Unix机器(AIX 5.3)运行该程序。

Help! 救命!

我的类路径指向的是commons-net jar文件的较旧版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM