FTP客户端 - 列表文件

时间:2011-01-10 09:29:02

标签: java ftp ftp-client apache-commons-net

我无法使用FTPClient获取确切的文件列表。示例代码如下:

FTPClient client = new FTPClient();
client.connect("x.x.x.x");
client.login("abcd", "abcd");
FTPFile[] ftpFiles = client.listFiles();
for (FTPFile ftpFile : ftpFiles) {
   System.out.println("FTPFile: " + ftpFile.getName());
}

我尝试使用enterLocalPassiveMode()/ enterRemotePassiveMode()/ pasv()设置为PASV模式。但是,它不起作用。

请同时查看Apache Commons FTPClient.listFiles ..

谢谢

2 个答案:

答案 0 :(得分:2)

我不知道files是什么,但您在client.listFiles中获得ftpFiles的结果,而不是files。然后在for循环中,您将看到files

答案 1 :(得分:1)


        试试这个。

String[] fileFtp = client.listNames();//if it is directory. then list of file names

//download file
for (int i =0;i<fileFtp.length;i++) {

   String fileName = fileFtp[i];

   OutputStream out = new FileOutputStream(new File("local temp file name"));

   if (!client.retrieveFile(fileName, out)) {       
        sysout("Could not download the file. "+ fileName);
    } else {
        sysout("Downloaded file @ : "+localFileName);   
    }       
} 

这应该有用 感谢。

相关问题