Apache Commons FTPClient.listFiles

坚强是说给别人听的谎言 提交于 2019-11-29 06:04:36

问题


I am using org.apache.commons.net.ftp.FTPClient in one of my applications to work with a FTP server. I am able to connect, login, pwd and cwd. However, when I try to list the files it doesn't return the list of files in that directory, where I know for sure that there are files. I am using the method FTPFile[] listFiles(), it returns an empty array of FTPFile.

Please find below the code snippet where I am trying this:

        String hostname = properties.getProperty("FTP_SERVER");
        String user = properties.getProperty("FTP_USER");
        String passwd = properties.getProperty("FTP_PASSWD");
        FTPClient client = new FTPClient();
        client.connect(hostname);
        client.login(user, passwd);
        String reply = client.getStatus();
        System.out.println(reply);
        client.enterRemotePassiveMode();
        client.changeWorkingDirectory("/uploads");
        FTPFile[] files = client.listFiles();
        System.out.println(files.length);
        for (FTPFile file : files) {
            System.out.println(file.getName());
        }

        String[] fileNames = client.listNames();
        if (fileNames != null) {
            for (String file : fileNames) {
                System.out.println(file);
            }
        }
        client.disconnect();

回答1:


This seems like the same issue I had (and solved), see this answer:

Apache Commons Net FTPClient and listFiles()




回答2:


After I set the mode as PASV it is working fine now! Thanks for all your efforts and suggestions!




回答3:


Just a silly suggestion... can you do a listing on the /uploads folder using a normal FTP client. I ask this because some FTP servers are setup to not display the listing of an upload folder.




回答4:


First, make sure the listing works in other programs. If so, one possibility is that the file listing isn't being parsed correctly. You can try explicitly specifying the parser to use with initiateListParsing.




回答5:


I had to same problem and it turned out to be that it couldn't parse what the server was returning for a file listing. I this line after connecting to the ftp server ftpClient.setParserFactory(new MyFTPFileEntryParserFactory());

public class MyFTPFileEntryParserFactory implements FTPFileEntryParserFactory {
private final static FTPFileEntryParser parser = new UnixFTPEntryParser() {
    @Override public FTPFile parseFTPEntry(String entry) {
        FTPFile ftpFile = new FTPFile();
        ftpFile.setTimestamp(getCalendar(entry));
        ftpFile.setSize(get(entry));
        ftpFile.setName(getName(entry));
        return ftpFile;
    }
};

@Override public FTPFileEntryParser createFileEntryParser(FTPClientConfig config) throws ParserInitializationException {
    return parser;
}

@Override public FTPFileEntryParser createFileEntryParser(String key) throws ParserInitializationException {
    return parser;
}

}




回答6:


I added client.enterLocalPassiveMode() and it works:

client.connect("xxx.com");
boolean login = client.login("xxx", "xxx");
client.enterLocalPassiveMode();


来源:https://stackoverflow.com/questions/2339855/apache-commons-ftpclient-listfiles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!