java.lang.NullPointerException使用listFiles方法从FTP下载文件 [英] java.lang.NullPointerException Downloading file from FTP with listFiles method
本文介绍了java.lang.NullPointerException使用listFiles方法从FTP下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想让所有以.xls扩展名出现在ftp上的文件名.
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();
}
我已确保ftp上存在文件:
I have made sure that files are present on ftp:
但是在线出现错误:
downloadFiles = ftp.listFiles();
我遵循以下语法:
http://kodejava. org/how-do-i-get-file-lists-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)
推荐答案
用户下面的代码获取文件列表
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.
}
这篇关于java.lang.NullPointerException使用listFiles方法从FTP下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文