【问题标题】:commons-net FTPClient.retrieveFileStream() returns wrong resultcommons-net FTPClient.retrieveFileStream() 返回错误结果
【发布时间】:2013-12-15 11:57:16
【问题描述】:

commons-net FTPClient 有问题。如果我从我的 ftp 服务器使用 retrieveFileStream() 下载文件,它可以工作,但我得到结果'150 Opening BINARY mode data connection for ...'。如果我调用 noop() 我得到“226 传输完成”作为结果。对于接下来的每个操作,我都会得到上一个操作的结果。

我发现,FTPClient 读取结果直到行尾,如果有两个结果行(如retrieveFileStream() 之后),我会在下一个命令之后得到第二个。我通过像这样覆盖 FTPClient.retrieveFileStream() 做了一个解决方法:

public static void main(String[] args){
    try {
        MyFTPClient ftpClient = new MyFTPClient();
        try {
            ftpClient.connect(ftphost, 21);
            if(!ftpClient.login( ftpuser, ftppassword )){
                throw new RuntimeException(ftpClient.getReplyString());
            }
            if(!ftpClient.changeWorkingDirectory("in")){
                throw new RuntimeException(ftpClient.getReplyString());
            }
            FTPFile[] files = ftpClient.listFiles();
            for(FTPFile file: files){
                if(file.getName().startsWith(FILENAME) && (file.getType() == FTPFile.FILE_TYPE)){
                    InputStream in = ftpClient.retrieveFileStream(file.getName());
                    CsvFile csvFile = new CsvFile(in, "ISO-8859-1", ';', "yyyyMMdd", "#.00", Locale.US, false);
                    in.close();
                    in = null;
                    System.out.println(ftpClient.getReplyString());
                    System.out.println(ftpClient.readLine());
                    System.out.println(ftpClient.rnfr(file.getName()));
                    System.out.println(ftpClient.getReplyString());
                    System.out.println(ftpClient.rnto("x" + file.getName()));
                    System.out.println(ftpClient.getReplyString());

                }
            }
            if(!ftpClient.logout()){
                throw new RuntimeException(ftpClient.getReplyString());
            }
        } finally {
            ftpClient.disconnect();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}

private static class MyFTPClient extends FTPClient{
    public String readLine(){
        try {
            __getReplyNoReport();
        } catch (IOException e) {
        }
        return getReplyString();
    }
}

方法 readLine() 的调用让我获得了额外的结果行。

但这是 FTPClient 的错误还是我的 ftp 服务器的问题?该解决方法的问题是,如果只有一行响应,该方法会阻塞。

感谢您的帮助

斯蒂芬

【问题讨论】:

  • 之前的代码示例错误,我替换了它。
  • 阅读手册有时会有所帮助。调用 completePendingCommand() 有效。
  • 您应该将其发布为答案并接受它。在 SO 上回答你自己的问题是完全可以的。

标签: java ftp apache-commons apache-commons-net


【解决方案1】:

阅读手册有时会有所帮助。调用 completePendingCommand() 有效

【讨论】:

  • 我认为你不应该因为错过这个而对自己如此苛刻,这完全不明显,我在使用.retrieveFileStream时也错过了它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-27
  • 2021-05-05
  • 2020-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多