Java Apache FTPClient 在第一个文件请求错误后请求第二个文件时停止

Java Apache FTPClient stalls when requesting a second file after a bad first file request

我正在尝试在 Java 中使用 FTPClient,并且在检索单个正确文件时可以正常工作。大约需要 1 秒,然后正确继续。现在,如果它的远程路径不好,我正试图让它恢复。

所以... retrieveFile(badPath) -> 大约需要 1 分钟告诉我:550 找不到文件或权限问题。

我发送了另一个 retrieveFile(goodPath) -> 命令,150 打开二进制模式数据连接。就呆在那里!有任何想法吗?

请注意,两个正确的请求也可以正常工作。是否需要对 550 进行某种特殊恢复?

public class FTPFetch
{

// Creating FTP Client instance
FTPClient ftp = null;

// Constructor to connect to the FTP Server
public FTPFetch(String host, int port, String username, String password) throws Exception
{

    ftp = new FTPClient();
    ftp.setConnectTimeout(5000);
    ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
    int reply;
    ftp.connect(host, port);
    reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply))
    {
        ftp.disconnect();
        throw new Exception("Exception in connecting to FTP Server");
    }
    ftp.login(username, password);
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
    ftp.enterLocalPassiveMode();
}

// Download the FTP File from the FTP Server
public boolean downloadFTPFile(String source, String destination)
{
    try (FileOutputStream fos = new FileOutputStream(destination))
    {
        if (this.ftp.retrieveFile(source, fos))
        {
            return true;
        } else
        {
            return false;
        }
    } catch (IOException e)
    {
        return false;
    }
}
// Disconnect the connection to FTP
public void disconnect()
{
    if (this.ftp.isConnected())
    {
        try
        {
            this.ftp.logout();
            this.ftp.disconnect();
        } catch (IOException f)
        {
            // do nothing as file is already saved to server
        }
    }
}
}

我是这样称呼它的:

if (!ftpobj.downloadFTPFile(s, temp[temp.length - 1]))
                {
                    if (s.contains("bd0"))
                    {
                        File f2 = new File(temp[temp.length - 1]);
                        errorLog.warn("/bd0 path not found on remote CCU. Attempting /bd5.");
                        ftpobj.downloadFTPFile(s.replace("bd0", "bd5"), temp[temp.length - 1]);
                    } else
                    {
                        File f2 = new File(temp[temp.length - 1]);
                        errorLog.warn("/bd5 path not found on remote CCU. Attempting /bd0.");
                        ftpobj.downloadFTPFile(s.replace("bd5", "bd0"), temp[temp.length - 1]);
                    }
                }

我的输出:

230 User logged in
TYPE I
200 Type set to I, binary mode
PASV
227 Entering Passive Mode (10,20,40,21,19,117)
RETR /bd5/CU_Anthony/PN1008TestDir.zip
550 File "/bd5/CU_Anthony/PN1008TestDir.zip" not found or permission problem
[WARN ] 03-14-2017 12:48:26 [main] DataAdapterFB1 - /bd5 path not found on      remote CCU. Attempting /bd0.
PASV
227 Entering Passive Mode (10,20,40,21,19,117)
RETR /bd0/CU_Anthony/PN1008TestDir.zip
150 Opening BINARY mode data connection

在服务器端,我看到在第二次请求后发生了数据连接错误,但我没有说明原因:

0x1890f20 (tFtpdServ0): (3170) data xfer failed: read 4096 bytes, wrote -1 bytes
0x1890f20 (tFtpdServ0): ftpdCmdSend: <426 Data connection error>
0x1890f20 (tFtpdServ0): (4136) sent 426 Data connection error

0x1890f20 (tFtpdServ0): (3413) Closing sock 55

我不确定它为什么会起作用,但是将其置于本地主动模式而不是本地被动模式后,它起作用了。