【问题标题】:apache commons net - completependingcommand returning falseapache commons net - completependingcommand 返回 false
【发布时间】:2012-12-27 18:58:08
【问题描述】:

我正在使用 apache commons net 库从 FTP 服务器获取文件。

我不需要下载整个文件,只需读取标题以确定文件大小。我用来做这个的库是metadata extractor

问题是当我调用 client.completePendingCommand() 它总是返回 false - 但是日期变量打印正确。我问过元数据提取器的开发人员,他不知道为什么它返回错误。有人有解释吗?我不确定是否可以忽略错误?

FTPClient client = new FTPHTTPClient(proxy settings);
InputStream stream = null;
try {
        client.connect(FTPProperties.getInstance().getProperty("ftp.server"));
        client.login(FTPProperties.getInstance().getProperty("ftp.username"), FTPProperties.getInstance().getProperty("ftp.password"));
        client.enterLocalPassiveMode();

        for (String path : paths) { //paths are the jpeg files to download
            try {
                stream = client.retrieveFileStream(p);

                Metadata metadata = ImageMetadataReader.readMetadata(stream);
                Directory directory = metadata.getDirectory(ExifSubIFDDirectory.class);
                Date date = directory.getDate(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL);
                System.out.println("DATE " + date);
            } catch (IOException ex) {
                Logger.getLogger(UploadImage.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                if(stream != null) {
                  stream.close();
                }
                if (in != null) {
                    in.close();
                }
                if (!client.completePendingCommand()) {
                     Logger.getLogger("Error");
                }
            }
        }
    } catch (Exception ex) {
        Logger.getLogger(UploadImage.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (client != null && client.isConnected()) {
            client.disconnect();
        }
    }

【问题讨论】:

  • 我刚刚发现completePendingCommand应该在retrieve|storeFileStream的流关闭后调用。

标签: java ftp apache-commons-net


【解决方案1】:

我不认为你做错了什么,我不认为元数据提取器有什么问题。您最好检查您正在检索的流是否可以正确处理,而不是使用 completePendingCommand() 作为成功的指示。如果出现问题,元数据提取器可能已经通过抛出异常来为您执行此操作。

解释: completePendingCommand() 验证整个事务是否成功,成功或失败取决于 FTPClients 回复代码是否在 200 http://commons.apache.org/proper/commons-net/apidocs/src-html/org/apache/commons/net/ftp/FTPReply.html#line.133) 范围内。

我遇到了类似的问题,发现我的 FTPClient 对象的回复码是 150,表示根据 FTP 服务器,事务还没有完成。回复代码 150 是肯定的初步回复,但不属于肯定的完成回复(https://www.rfc-editor.org/rfc/rfc959 第 37 页)。我的观点是响应仍然是积极的,虽然我认为我已经完成了交易,但 FTP 服务器仍然认为我需要做点什么。这可能是 org.apache.commons.net.ftp.FTPClient 或与之交互的 FTP 服务器的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 2023-04-05
    • 2014-12-10
    • 2015-01-17
    相关资源
    最近更新 更多