我想列出位于ftp服务器目录中的文件,但我有一些错误,如:
drwxrwxrwx 3 1004 1004 4096 Jan 17 2017 ftp
Exception in thread "main" java.lang.NullPointerException
我的代码:
URL url = new URL("ftp://username:pass@hostname:port");
URLConnection urlConnection = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null){
System.out.println(inputLine);
}
File cur = new File("/ftp/files/assignment/");
getAllFiles(cur);}}
}
public static void getAllFiles(File curDir){
long len = 0;
long time = curDir.lastModified();
File[] filesList = curDir.listFiles();
for(File f : filesList){
len += f.length();
if(f.isDirectory())
{
System.out.println(f.getName() + " " + len + " " + time);
}
if(f.isFile()){
System.out.println(f.getName() + " " + len + " " + time);
}
}
}
答案 0 :(得分:0)
File
仅适用于本地文件,而不适用于远程FTP服务器上的文件,并且本地计算机上不存在/ftp/files/assignment/
(listFiles()
返回null导致你的for循环崩溃。)
From https://docs.oracle.com/javase/7/docs/api/java/io/File.html#listFiles() - “如果此抽象路径名不表示目录,或者发生I / O错误,则返回null。”