Raghul Raman
Raghul Raman

Reputation: 199

FTPClient FTPFile details not available

I am trying to get the list of files from remote FTP server. The ftpClient.listFiles() returned null and I had to set setUnparseableEntries to true to get the list of files. Even then the list of files do not have any information like name and only information it has is rawlisting and others are null. So I cannot do ftpFile.getName. Here is the code

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;
}

Following method is trying to get the list of files. I could see the file name using listNames and also listFiles shows list of files but name, modified date are empty and only has value in rawlisting in the format "04-01-20 11:31AM 8975 test.TXT". So how to get the name and modified date from the raw listing and why I could not get FTPFile name using getName

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;

}

Upvotes: 1

Views: 533

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202360

04-01-20 11:31AM 8975 test.TXT

That's quite unusual format. So it's possible that Apache Commons Net library cannot parse it with the default configuration.

You might need to explicitly specify one of the available parsers. The available parsers are in src\main\java\org\apache\commons\net\ftp\parser. Or if there's no parser specifically compatible with your server, you might need to build your own (you can base it on ConfigurableFTPFileEntryParserImpl).

Though actually, for an ad-hoc solution, easier would be that you just parse the "rawlisting" you already have.

Upvotes: 1

Related Questions