当前位置: 首页 > 知识库问答 >
问题:

Java FTPS无法检索文件列表(FileZilla客户端工作正常)

金和雅
2023-03-14

我使用Apache Commons Net(v3.5)和Java8连接到一个远程FTPS站点(即在internet上)。我可以轻松地与Windows10机器上的FileZilla客户端连接,但我的Java程序无法完成相同的步骤。我在谷歌上搜索了很多,但找不到根本原因。以下是我已经确认的事情:

  • 我确保Java FTP命令的顺序与FileZilla客户端完全相同。
  • 我禁用了PC上的Windows防火墙和防病毒
  • 我重新启用了Windows防火墙并启用了日志记录。使用FileZilla时,Windows防火墙日志列出了建立被动模式连接时的TCP连接。我在Java程序中没有看到这样的条目。
  • 我在PC上安装了FileZilla服务器。在我取消了“使用PROT P时需要在数据连接上恢复TLS会话”的勾选后,java程序工作了。java异常是不同的,所以我不相信这是一个确凿的证据。
  • 我成功地在test.rebex.com服务器上运行了相同的代码。

以下是代码,任何想法都非常感谢:

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.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;

public class testProgram {

  public static void main(String[] args) {

    String ftpServer = "ftp.domain.com";
    String ftpUsername = "user@domain.com";
    String ftpPassword = "********";

    FTPSClient ftp = null;

    // CONNECT TO THE SERVER
    try {
        // I have tried "SSL" as the argument, but same result
        ftp = new FTPSClient(); 
        ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));

        ftp.connect(ftpServer,21);

        int reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            System.err.println("---------->FTP server refused connection.\n");

        } 

    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();

    }

    // LOGIN INTO SERVER
    try {
        if (!ftp.login(ftpUsername, ftpPassword)) {
            ftp.logout();

        } else {

            ftp.sendCommand("OPTS UTF8 ON");            
            ftp.execPBSZ(0);            
            ftp.execPROT("P");
            ftp.pwd();
            ftp.setFileType(FTP.BINARY_FILE_TYPE);      
            ftp.enterLocalPassiveMode();

            /* The next command always fails.

               The FTP Server responds with "150 Accepted data connection" then:

                org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication.
                at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:316)
                at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:292)
                at org.apache.commons.net.ftp.FTP.getReply(FTP.java:712)
                at org.apache.commons.net.ftp.FTPClient.completePendingCommand(FTPClient.java:1857)
                at org.apache.commons.net.ftp.FTPClient.listNames(FTPClient.java:2919)
                at org.apache.commons.net.ftp.FTPClient.listNames(FTPClient.java:2952)
                at myPackage.testProgram.main(testProgram.java:78)

                I have tried other commands, but it disconnects here...
             */

            FTPFile[] ftpFiles = ftp.listFiles();
            System.out.println("---------->Number of Files = " + ftpFiles.length);
            ftp.logout();

        }
    } catch (Exception e) {

        e.printStackTrace();
    } 

    //Ensure Disconnected at the end.
    if (ftp.isConnected()) {
        try {
            ftp.disconnect();
        } catch (IOException f) {
            // do nothing
        }

    }
  }
}
2016-09-06 09:09:50 4756 1 Status: Resolving address of ftp.domain.com
2016-09-06 09:09:51 4756 1 Status: Connecting to h1.h2.h3.h4:21...
2016-09-06 09:09:51 4756 1 Status: Connection established, waiting for welcome message...
2016-09-06 09:09:51 4756 1 Response: 220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
2016-09-06 09:09:51 4756 1 Response: 220-You are user number 2 of 50 allowed.
2016-09-06 09:09:51 4756 1 Response: 220-Local time is now 13:09. Server port: 21.
2016-09-06 09:09:51 4756 1 Response: 220-This is a private system - No anonymous login
2016-09-06 09:09:51 4756 1 Response: 220-IPv6 connections are also welcome on this server.
2016-09-06 09:09:51 4756 1 Response: 220 You will be disconnected after 15 minutes of inactivity.
2016-09-06 09:09:51 4756 1 Command: AUTH TLS
2016-09-06 09:09:51 4756 1 Response: 234 AUTH TLS OK.
2016-09-06 09:09:51 4756 1 Status: Initializing TLS...
2016-09-06 09:09:51 4756 1 Status: Verifying certificate...
2016-09-06 09:09:51 4756 1 Status: TLS connection established.
2016-09-06 09:09:51 4756 1 Command: USER user@domain.com
2016-09-06 09:09:51 4756 1 Response: 331 User user@domain.com OK. Password required
2016-09-06 09:09:51 4756 1 Command: PASS *************
2016-09-06 09:09:51 4756 1 Response: 230 OK. Current restricted directory is /
2016-09-06 09:09:51 4756 1 Command: SYST
2016-09-06 09:09:51 4756 1 Response: 215 UNIX Type: L8
2016-09-06 09:09:51 4756 1 Command: FEAT
2016-09-06 09:09:51 4756 1 Response: 211-Extensions supported:
2016-09-06 09:09:51 4756 1 Response:  EPRT
2016-09-06 09:09:51 4756 1 Response:  IDLE
2016-09-06 09:09:51 4756 1 Response:  MDTM
2016-09-06 09:09:51 4756 1 Response:  SIZE
2016-09-06 09:09:51 4756 1 Response:  MFMT
2016-09-06 09:09:51 4756 1 Response:  REST STREAM
2016-09-06 09:09:51 4756 1 Response:  MLST type*;size*;sizd*;modify*;UNIX.mode*;UNIX.uid*;UNIX.gid*;unique*;
2016-09-06 09:09:51 4756 1 Response:  MLSD
2016-09-06 09:09:51 4756 1 Response:  AUTH TLS
2016-09-06 09:09:51 4756 1 Response:  PBSZ
2016-09-06 09:09:51 4756 1 Response:  PROT
2016-09-06 09:09:51 4756 1 Response:  UTF8
2016-09-06 09:09:51 4756 1 Response:  TVFS
2016-09-06 09:09:51 4756 1 Response:  ESTA
2016-09-06 09:09:51 4756 1 Response:  PASV
2016-09-06 09:09:51 4756 1 Response:  EPSV
2016-09-06 09:09:51 4756 1 Response:  SPSV
2016-09-06 09:09:51 4756 1 Response:  ESTP
2016-09-06 09:09:51 4756 1 Response: 211 End.
2016-09-06 09:09:51 4756 1 Command: OPTS UTF8 ON
2016-09-06 09:09:51 4756 1 Response: 200 OK, UTF-8 enabled
2016-09-06 09:09:51 4756 1 Command: PBSZ 0
2016-09-06 09:09:51 4756 1 Response: 200 PBSZ=0
2016-09-06 09:09:51 4756 1 Command: PROT P
2016-09-06 09:09:52 4756 1 Response: 200 Data protection level set to "private"
2016-09-06 09:09:52 4756 1 Status: Logged in
2016-09-06 09:09:52 4756 1 Status: Retrieving directory listing...
2016-09-06 09:09:52 4756 1 Command: PWD
2016-09-06 09:09:52 4756 1 Response: 257 "/" is your current location
2016-09-06 09:09:52 4756 1 Command: TYPE I
2016-09-06 09:09:52 4756 1 Response: 200 TYPE is now 8-bit binary
2016-09-06 09:09:52 4756 1 Command: PASV
2016-09-06 09:09:52 4756 1 Response: 227 Entering Passive Mode (h1,h2,h3,h4,133,150)
2016-09-06 09:09:52 4756 1 Command: MLSD
2016-09-06 09:09:52 4756 1 Response: 150 Accepted data connection
2016-09-06 09:09:52 4756 1 Response: 226-Options: -a -l 
2016-09-06 09:09:52 4756 1 Response: 226 6 matches total

根据Mike的建议,我打开了TLS调试。程序似乎再次通过TLS握手。输出很长,但在发出list命令后,我看到“***ClientHello,tlsv1.2”以及与启动FTP连接类似的命令。

差别似乎出现在最后:

%% Cached client session: [Session-2, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256]
main, received EOFException: ignored
main, called closeInternal(false)
main, SEND TLSv1.2 ALERT:  warning, description = close_notify
main, WRITE: TLSv1.2 Alert, length = 26
main, called closeSocket(false)
main, called close()
main, called closeInternal(true)
main, called close()
main, called closeInternal(true)
main, received EOFException: ignored
main, called closeInternal(false)
main, SEND TLSv1.2 ALERT:  warning, description = close_notify
main, WRITE: TLSv1.2 Alert, length = 26
main, called closeSocket(false)
org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication.

共有1个答案

呼延聪
2023-03-14

虽然这看起来像是一个老帖子,但我今天面临着一个类似的问题,(最初)想不出解决方案。我可以通过FileZilla进行连接,但不能使用FTPSClient进行连接,并且在运行FtpClient.EnterLocalPassiveMode()之后,我使用了425无法打开数据连接

我的解决方案是在登录之前更改ftpclient.EnterLocalPassiveMode(),但在连接到FTPServer之后更改它并使其工作。通常,我看到的所有代码示例在发送/接收数据之前但在登录之后都使用EnterLocalPassiveMode。请参阅下面的代码,以获得一个适用于我的示例。

FTPSClient ftpClient = new FTPSClient(false);
ftpClient.connect("remote.ftp.server", port);
ftpClient.enterLocalPassiveMode();// Run the passive mode command now  instead of after loggin in.
ftpClient.login("username", "password");
ftpClient.execPBSZ(0);
ftpClient.execPROT("P");
ftpClient.type(FTP.BINARY_FILE_TYPE);
//ftpClient.enterLocalPassiveMode(); Previously it was here.
FTPFile[] files = ftpClient.listDirectories("/");

希望这有帮助。还请注意,为了保持答案简短,省略了所有其他代码和良好实践。

 类似资料:
  • FileZilla是一个开源的FTP客户端,可以在Linux和Windows等多种平台上使用。 此客户端还支持通过SFTP和FTPS的安全连接进行FTP。 您可以使用此软件在网络服务器中上传和管理文件。 当您希望上传您的网站时,文件数量或文件大小都非常大。 然后你可以使用FTP上传,因为通过cPanel文件管理器上传一个非常大的文件会导致上传错误。 大多数cPanel帐户都附带系统FTP帐户,您可

  • 我有一个wsdl: 我想提交信息以获得回应。我创建了client.php如下: 但它在浏览器中显示错误: SoapFault对象([消息:受保护]= 我错在哪里?对此,可能的解决方案是什么? 编辑: 我已经创建了一个php文件:client。php 但它产生了这个错误: 调用错误:响应不是文本/xml类型:应用程序/wsdl xmlHTTP/1.1 200确定日期:星期二,9月17日2013 15

  • 我目前正在创建一个基于此小提琴的动态下拉列表 除了我试图调用JSON文件之外,我一直在跟进。我的代码如下: ]; 范围metro可以工作,但是当连接到JSON文件时,其余的就不能工作了。我假设它会工作,因为它实际上是相同的结构。我做错了什么? [编辑] 正如你们中的一些人所回答的那样,在我进行上述操作之前,我最初尝试了这种http注入: 这不起作用,所以我试了另一种方法。所以我假设它与JSON赋值

  • 我有一个名为search2的集合,里面有大约20000个这样的文档: 集合上只有这个索引和“_id”字段上的默认索引。 当我执行以下查询时,我希望“nscannedObjects”为=10: 但结果是这样的: 提前致谢

  • 我有一个很长的表单,表单的内容是通过邮件(HTML电子邮件)发送的。 我的PHP邮件函数在其余的内容中工作得很好。但当我将以下内容添加到邮件正文时,邮件就不工作了。 基本上,我得到的。 邮件头为

  • 问题内容: 我的工作区中有一个文件夹,但在“ src”之外,其中包含一个文件,使用自定义插件向导时,该文件需要读取才能设置新文件。 除非我确切指定该文件在系统中的位置,否则我无法正确获取此文件的位置并保持获取空指针。我的问题是该文件在插件项目中,但无法获取它的位置。 插件中的文件位置为com.my.plugin / rules / setup.txt 问题答案: 要从已部署的捆绑包中加载资源,您可