[ 
https://issues.apache.org/jira/browse/NET-745?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18098034#comment-18098034
 ] 

Sarankumar Baskar commented on NET-745:
---------------------------------------

[~msepryor]  Here is the detailed explanation, 

I reproduced this locally and confirmed the issue. A few findings:

1. OS400FTPEntryParser does not match your listings at all — your server is 
returning Unix-style output, so UnixFTPEntryParser is the parser being used.

2. UnixFTPEntryParser intentionally preserves leading spaces in filenames by 
default, because Unix filenames can legitimately start with spaces.

3. The leading space you see comes from date column alignment: when the listing 
shows a year like "2025" instead of a time like "15:46", there is an extra 
space before the filename.

The fix is to enable trimLeadingSpaces when creating the FTP client. Here are 
two ways to do it:

Option 1: Configure the parser directly

    FTPClient ftpClient = new FTPClient();
    ftpClient.connect(server);
    ftpClient.login(user, password);

    FTPClientConfig config = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
    ftpClient.configure(config);

    ftpClient.setParserFactory(new DefaultFTPFileEntryParserFactory() {
        @Override
        public FTPFileEntryParser createUnixFTPEntryParser() {
            return new UnixFTPEntryParser(config, true); // 
trimLeadingSpaces=true
        }
    });

Option 2: Trim the name after parsing

    FTPFile[] files = ftpClient.listFiles(remotePath);
    for (FTPFile file : files) {
        String name = file.getName().trim();
        // use trimmed name
    }

Option 2 is simpler if you just need clean filenames. Option 1 fixes it at the 
parser level.

This feature was added in Commons Net 3.4. Make sure you are using 3.4 or later.

Hope this helps.

Thanks,
Sarankumar

> OS400FTPEntryParser returns filename with leading space when listing contains 
> a year instead of time
> ----------------------------------------------------------------------------------------------------
>
>                 Key: NET-745
>                 URL: https://issues.apache.org/jira/browse/NET-745
>             Project: Commons Net
>          Issue Type: Bug
>            Reporter: Sarankumar Baskar
>            Priority: Major
>
> OS400FTPEntryParser produces a filename with a leading space when the FTP 
> directory listing contains a year (previous-year entries) instead of a time 
> (current-year entries).
> In standard Unix-style directory listings, the date/time column is 
> fixed-width. When the listing shows a year like "2025" instead of a time like 
> "15:46", the output is padded with an extra space to maintain column 
> alignment. The parser's regex captures that extra space as part of the 
> filename.
> Reproduction:
> Current-year entry (parsed correctly):
>   Raw:  "-rwxrwxrwx   1 TLXDRIVER 0        1592686 Jul 14 15:46 cloudfax.jar"
>   Name: "cloudfax.jar"
> Previous-year entry (leading space in name):
>   Raw:  "-rwxrwxrwx   1 TLXDRIVER 0        27432520 Apr 24  2025  
> itextpdf-5.2.1.jar"
>   Name: " itextpdf-5.2.1.jar"
> Expected: FTPFile.getName() should return "itextpdf-5.2.1.jar" without a 
> leading space.
> Suggested fix: adjust the regex in OS400FTPEntryParser to allow one or more 
> whitespace characters before the filename capture group, or trim the captured 
> filename.
> Reported on dev mailing list by Matt Pryor.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to