【问题标题】:FTPClient FTPFile details not availableFTPClient FTPFile 详细信息不可用
【发布时间】:2020-09-16 08:08:30
【问题描述】:

我正在尝试从远程 FTP 服务器获取文件列表。 ftpClient.listFiles() 返回null,我必须将setUnparseableEntries 设置为true 才能获取文件列表。即使那样,文件列表也没有任何信息,如名称,只有它拥有的信息是原始列表,其他信息为空。所以我不能做ftpFile.getName。这是代码

public FTPFile[] process() throws Exception {
 String message = null;
 FTPFile[] files = null;

 FTPClient ftpClient = new FTPClient();

 FTPClientConfig config = new FTPClientConfig();
 config.setServerTimeZoneId("America/Chicago");
 config.setUnparseableEntries(true);
 ftpClient.configure(config);

 if ( !connectToServer() ) return null;

 if ( !changeDirectory() ) {
    disconnectFromServer();
    return null;
 }
 files = getListofFiles();
 disconnectFromServer();

 return files;
}

private boolean connectToServer() {
 boolean result = true;
 String message = null, url = null;

 // attempt to connect to the target server
 try {
     url = fo.getServerInfo().getConnectionURL();
     LOGGER.debug("Connecting to: " + url);
     ftpClient.connect(fo.getServerInfo().getHostName(),
            fo.getServerInfo().getHostPort());
     ftpClient.enterLocalPassiveMode();
 } catch(SocketException e) {
     result = false;
     message = "Could not connect to server at " + url;
 } catch(IOException e) {
     result = false;
     message = "Could not connect to server at " + url;
 }
 if ( !result ) return result;

 // After connection attempt, you should check the reply code to verify success.
 Integer replyCode = ftpClient.getReplyCode();

 if ( !FTPReply.isPositiveCompletion(replyCode) ) {
    message = "Reply Code - " + replyCode.toString() + " is negative.";
    try {
        ftpClient.disconnect();
    } catch(Exception e) {
        message = "Could not disconnect cleanly from server.";
        LOGGER.error(message);
    }
 } else {
    message = "Reply Code - " + replyCode.toString() + " is positive.";
 }

 Boolean logonOk = false;
 try {
    logonOk = ftpClient.login(fo.getServerInfo().getUserName(), 
            fo.getServerInfo().getUserPassword());
 } catch(IOException e) {
    message = "IOException during logon attempt.";
    LOGGER.error(message);
 }
 if ( !logonOk ) {
    result = false;
    message = "Logon UNsuccessful.";
 } else {
    message = "Logon successful.";
    LOGGER.error(message);
    executionMessageLog.add(message);
 }

 if ( !result ) return result;

 // attempt to log onto the target server

 return result;
}

以下方法正在尝试获取文件列表。我可以使用listNames 看到文件名,listFiles 也显示文件列表,但名称、修改日期为空,并且仅在格式为“04-01-20 11:31AM 8975 test.TXT”的原始列表中具有值。那么如何从原始列表中获取名称和修改日期以及为什么我无法使用 getName 获取 FTPFile 名称

private FTPFile[] getListofFiles(){
 String message = null;
 FTPFile[] files = null;
 try {
    String[] filenames = ftpClient.listNames(fileListInfo.getFilePath());
    files = ftpClient.listFiles(); /*Has only rawlisting and others are null*/
 }
 catch(IOException e) {
    message = "IOException during getListofFiles attempt:";
    LOGGER.error(message);
    executionMessageLog.add(message);
    message = e.getMessage();
    LOGGER.error(message);
    executionMessageLog.add(message);
 }
 return files;

}

【问题讨论】:

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


    【解决方案1】:
    04-01-20 11:31AM 8975 test.TXT
    

    这是一种非常不寻常的格式。因此,Apache Commons Net 库可能无法使用默认配置对其进行解析。

    您可能需要明确指定可用的解析器之一。可用的解析器位于src\main\java\org\apache\commons\net\ftp\parser。或者,如果没有专门与您的服务器兼容的解析器,您可能需要构建自己的解析器(您可以基于 ConfigurableFTPFileEntryParserImpl)。

    虽然实际上,对于临时解决方案,您只需解析已有的“原始列表”就更容易了。

    【讨论】:

    • 我尝试了不同的解析器,其中一个似乎成功了,我在 FTPClientConfig 中指定了,现在我可以看到名称、大小和日期。
    猜你喜欢
    • 1970-01-01
    • 2018-06-27
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多