java.lang.NullPointerException使用listFiles方法从FTP下载文件

时间:2014-02-24 11:14:26

标签: java ftp

我想让ftp上的所有文件名都扩展为.xls。

因此我写了以下代码:

FTPClient ftp = new FTPClient();
FTPFile[] downloadFiles = null;
try {
    ftp.connect(Ftp_Path);
    ftp.login(ftpUserID, ftpPassword);

    downloadFiles = ftp.listFiles();

    xlsFiles = new ArrayList<String>();
    for(FTPFile i : downloadFiles) {
       if(i.toString().endsWith(".xls")) {
           xlsFiles.add(i.toString());
       }
    }

} catch (Exception e) {
    e.printStackTrace();
}

我确保文件存在于ftp:

enter image description here

但是在线获得错误:

downloadFiles = ftp.listFiles();

我遵循以下语法:

http://kodejava.org/how-do-i-get-list-of-files-from-ftp-server/

但是得到错误:

java.lang.NullPointerException
    at com.amazonaws.mws.samples.ImportRulesPropertyClass.GetFileList(ImportRulesPropertyClass.java:39)
    at com.amazonaws.mws.samples.ManageReportScheduleSample.main(ManageReportScheduleSample.java:74)

2 个答案:

答案 0 :(得分:1)

用户在代码下方获取文件列表

FTPClient f=FTPClient();
    f.connect(server);
    f.login(username, password);
    FTPListParseEngine engine = f.initiateListParsing(directory);

    while (engine.hasNext()) {
       FTPFile[] files = engine.getNext(25);  // "page size" you want
       //do whatever you want with these files, display them, etc.
       //expensive FTPFile objects not created until needed.
    }

答案 1 :(得分:0)

我补充说:

ftp.enterLocalPassiveMode();

之前的代码:

String downloadFiles[]=ftp.listNames("");

现在正在工作

相关问题