开发者

FTP client - listfiles

开发者 https://www.devze.com 2023-02-04 10:05 出处:网络
I am unable to get the exact file list using FTPClient. Sample code as below :开发者_如何学C FTPClient client = new FTPClient();

I am unable to get the exact file list using FTPClient. Sample code as below :开发者_如何学C

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

I tried to set to PASV mode using enterLocalPassiveMode()/enterRemotePassiveMode()/pasv(). But, it doesnt work.

Please also check Apache Commons FTPClient.listFiles ..

Thank you


I don't know what files is, but you're getting the results of client.listFiles in ftpFiles, and not in files. Then in your for loop you go over files.



Try this.

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

This should work.
Thanks.

0

精彩评论

暂无评论...
验证码 换一张
取 消