【问题标题】:apache commons net ftp sendCommand returns wrong reply codeapache commons net ftp sendCommand 返回错误的回复代码
【发布时间】:2017-03-08 14:27:42
【问题描述】:

我有一个 FTP 服务器,我正在尝试上传文件

在 FTPClient.openDataConnection() 中,我下线了

if (!FTPReply.isPositivePreliminary(sendCommand(command, arg))) {
    return null;
}

命令是“STOR”,arg是“edf0864d-1651-43b2-8453-d160bf9d0742”(我要存储的文件名)

服务器日志如下:

PORT 86,185,26,230,213,101
200 Port command successful
STOR edf0864d-1651-43b2-8453-d160bf9d0742
150 Opening data channel for file upload to server of "/edf0864d-1651-43b2-8453-d160bf9d0742"
(Up to here after calling sendCommand)
PORT 86,185,26,230,213,102
200 Port command successful

从日志中,响应码是 150,但 sendCommand 返回的响应码是 200

其他可能重要的信息

我正在使用 filezilla 服务器

服务器大部分时间都在工作,使用完全相同的代码。文件的初始上传工作正常,然后当我选择另一个文件覆盖第一次上传时,就会发生这种情况。

接着,还有另一个同名文件,但我之前没有遇到过问题。

据我所知,服务器配置为被动连接,当我启动它时它不会像以前那样对我大喊大叫,但是 PASV 命令会产生 421 无法创建套接字错误。我正在使用 ACTIVE_LOCAL 模式

我正在使用 apache commons net 3.6

如果有更多信息可以帮助我,请告诉我

这是java的相关部分:

FTPClient ftp = new FTPClient();
String server = <server ip>;
int port = 21;
String username = <username>;
String password = <password>;
try{
    ftp.connect(server, port);
}
catch(SocketException e){
    throw new FTPCouldNotConnectException("Threw SocketException during connection", e);
}
catch(IOException e){
    throw new FTPCouldNotConnectException("Threw IOException during connection", e);
}
int reply = ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)){
    try{
        ftp.disconnect();
        throw new FTPCouldNotConnectException("Reply Code: " + reply + ", Could not connect");
    }
    catch(IOException e){
        throw new FTPCouldNotConnectException(
                "Threw IOException during disconnection after failing to connect with reply code: " + reply,
                e);
    }
}
else{
    try{
        if(ftp.login(username, password)){
            LOGGER.info("Successfully logged in to ftp server");
            ftp.setFileTransferMode(org.apache.commons.net.ftp.FTP.STREAM_TRANSFER_MODE);
            ftp.setFileStructure(org.apache.commons.net.ftp.FTP.FILE_STRUCTURE);
            ftp.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
            ftp.enterLocalActiveMode();
        }
        else{
            LOGGER.warn("Failed to log in to ftp server");
        }
    }
    catch(IOException e){
        throw new FTPCouldNotConnectException("Failed to log in to server, threw ioexception", e);
    }
}
try{
    ftp.storeFile("edf0864d-1651-43b2-8453-d160bf9d0742", new FileInputStream("test.jpg"));
}
catch(IOException e){
    LOGGER.error("java.io.IOException caught", e);
}

【问题讨论】:

  • 请添加更多代码
  • 请准确添加您使用的库及其版本。
  • 添加了java代码和库版本
  • 使用 Wireshark 捕获这两种情况的 FTP 流量并向我们展示。
  • 添加了wireshark捕获

标签: java ftp apache-commons-net


【解决方案1】:

我根据代码的第一次调用运行良好,但第二次失败的事实找到了答案。

在 java 代码中,我没有调用 FTPClient.completePendingCommand(),它作为 The docs say 会导致后续调用出现意外行为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-11
    • 2012-12-27
    • 1970-01-01
    • 2013-12-15
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多