Apache Commons FTPClient Loggin

时间:2017-10-13 08:39:16

标签: java ftp-client ftps

我正在使用org.apache.commons.net.ftp.FTPSClient将文件上传到FTPS服务器(通过TLS / SSL)。

请注意,FTP服务器位于Windows服务器上,证书是自签名证书,可以连接并在我接受证书无效的警告后通过FileZilla成功上传文件。

但是处理相同内容的java代码不起作用。我已经在代码中单独检查了连接步骤和登录步骤,发现连接步骤(ftps.connect(服务器,端口))后发生了错误 - 在控制台中打印了“已连接到服务器:端口”日志。我很困惑,在连接步骤中应该处理getRemoteAddress,这已经完成了。

String ftpsServer = "host";
    int ftpsPort = 21;
    String ftpsUser = "domain\username";
    String ftpsPass = "password";

    try{
        FTPSClient ftpClient = new FTPSClient();
        ftpClient.connect(ftpsServer,ftpsPort);
        // check FTP connection
        int reply = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)){
            ftpClient.disconnect();
            System.err.println("FTP server refused connection.");
            System.exit(1);
            } else {
                System.out.println("Connected to " + ftpsServer + ":" + ftpsPort + ".");
            }
        // check FTP login
        if (ftpClient.login(ftpsUser, ftpsPass)){
            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            System.out.println("Logged into FTP server successfully");
        } else {
            System.out.println("Failed log into FTP server");
            ftpClient.logout();
            ftpClient.disconnect();
        }

        File localFile = new File("C:/test/history.txt");
        InputStream inputStream = new FileInputStream(localFile);
        boolean uploaded = ftpClient.storeFile("/wwwroot/preview/history.txt", inputStream);
        System.out.println("uploaded successful? " + uploaded);

        inputStream.close();
        ftpClient.logout();
        ftpClient.disconnect();

    }catch(Exception e){
        LOG.error("Exception when merging Reminderlist is: ", e);

    }

在控制台中打印的完整日志是:

Connected to 10.20.254.xx:21.
Failed log into FTP server
ERROR 2017-10-13 15:27:15,322 [main] 
com.redwood.contentmanagement.UnzipFiles: Exception when merging 
Reminderlist is: 
java.lang.NullPointerException
at 
org.apache.commons.net.SocketClient.getRemoteAddress(SocketClient.java:553) ~[commons-net-2.2.jar:2.2]
at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:509) ~[commons-net-2.2.jar:2.2]
at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:425) ~[commons-net-2.2.jar:2.2]
at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:1548) ~[commons-net-2.2.jar:2.2]
at com.redwood.contentmanagement.TransferOverFTPS.myTransform(TransferOverFTPS.java:55) ~[classes/:?]
at com.redwood.contentmanagement.TransferOverFTPS.main(TransferOverFTPS.java:110) ~[classes/:?]

非常感谢@ devpuh的回复,我在更改\ to / in username之后已成功登录,但仍无法上传文件,并且没有详细信息失败,下面是完整登录控制台:

Connected to 10.20.254.10:21.
Logged into FTP server successfully
uploaded successful? false

1 个答案:

答案 0 :(得分:0)

原始问题

您的登录凭据不正确,但您仍在尝试上传文件。

您必须正确处理错误登录。例如:

// check FTP login
if (ftpClient.login(ftpsUser, ftpsPass)){
    ftpClient.enterLocalPassiveMode();
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    System.out.println("Logged into FTP server successfully");
} else {
    System.out.println("Failed log into FTP server");
    ftpClient.logout();
    ftpClient.disconnect();
    return;
}

针对已修改的问题

要了解上传无效的原因,您可以使用getReplyString()从服务器获取最后一个回复。例如:

boolean uploaded = ftpClient.storeFile("/wwwroot/preview/history.txt", inputStream);
if(!uploaded) {
    System.err.println("Can't upload File! Reply from Server: " + ftpClient.getReplyString());
} else {
    System.out.println("Upload successful");
}

PS:FTPClient很少显示错误或警告消息,因此最好添加PrintCommandListener进行调试。

ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
相关问题