简体   繁体   中英

java.lang.NullPointerException Downloading file from FTP with listFiles method

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)

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

I added:

ftp.enterLocalPassiveMode();

in code before:

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

Its working now

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM