java FTP 客户端 - 列表文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4645286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FTP client - listfiles
提问by Raghu
I am unable to get the exact file list using FTPClient. Sample code as below :
我无法使用 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());
}
I tried to set to PASV mode using enterLocalPassiveMode()/enterRemotePassiveMode()/pasv(). But, it doesnt work.
我尝试使用 enterLocalPassiveMode()/enterRemotePassiveMode()/pasv() 设置为 PASV 模式。但是,它不起作用。
Please also check Apache Commons FTPClient.listFiles..
另请检查Apache Commons FTPClient.listFiles..
Thank you
谢谢
回答by Eldad Mor
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
.
我不知道是什么files
,但你得到的结果是client.listFiles
in ftpFiles
,而不是 in files
。然后在你的for
循环中你过去了files
。
回答by nIKUNJ
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.
这应该有效。
谢谢。