【问题标题】:FTPClient is returning: Cannot parse response codeFTPClient 正在返回:无法解析响应代码
【发布时间】:2020-04-27 20:58:38
【问题描述】:

当我尝试使用 FTPClient 检索文件时收到此消息:

Could not parse response code.
Server Reply:
150 Opening data channel for file download from server

我是如何连接的:

    FTPClient ftpClient = new FTPClient();
    ftpClient.setDefaultTimeout(this.timeout);

    try {
        ftpClient.connect(this.host, this.port);
        ftpClient.setSoTimeout(this.timeout);
        if (!ftpClient.login(username, password)) {
            return null;
        }
    } catch (IOException e) {
        return null;
    }

导致问题的代码是:

    File downloadedFile = new File(localFile);

    try (OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadedFile))) {

        ftpClient.retrieveFile(remoteFilePath, outputStream);
    }

当我通过 FTP 命令手动下载文件时,收到多行回复:

ftp> get file
local: file remote: file
200 Port command successful
150 Opening data channel for file download from server of "/file"
226 Successfully transferred "/file"
202 bytes received in 0.00 secs (1777.2 kB/s)

【问题讨论】:

标签: java ftp apache-commons


【解决方案1】:

可能是在传输完成之前连接已关闭或传输方法无效。

  • 能否尝试通过 Filezilla 客户端下载文件来测试它是否有效?
  • 你能分享你用来连接 FTP 的代码吗?

我查看了我之前编写的代码,我必须在下载文件之前添加以下行。试试这个:

import org.apache.commons.net.ftp.FTP;

try (OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadedFile))) {
    // These lines set the type of file to be binary.
    // I had some issues that the file downloaded corrupted.
    ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
    ftpClient.setFileStructure(FTP.FILE_STRUCTURE);
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    ftpClient.setConnectTimeout(timeout);

    ftpClient.retrieveFile(remoteFilePath, outputStream);
}

另外,检查超时值。

【讨论】:

    猜你喜欢
    • 2013-03-15
    • 1970-01-01
    • 2017-05-27
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多