java.lang.NullPointerException Downloading file fr

2019-09-16 19:38发布

问题:

I wanted to get all fileNames present on ftp with .xls extension.

Hence i written following code:

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

I have made sure that files are present on ftp:

But getting error on line:

downloadFiles = ftp.listFiles();

I followed syntax from :

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

But getting error:

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

回答1:

user below code to get files list

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


回答2:

I added:

ftp.enterLocalPassiveMode();

in code before:

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

Its working now



标签: java ftp