Java Apache Commons FTPClient.listFiles
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2339855/
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
Apache Commons FTPClient.listFiles
提问by Shyam
I am using org.apache.commons.net.ftp.FTPClient
in one of my applications to work with a FTP server. I am able to connect
, login
, pwd
and cwd
. However, when I try to list
the files it doesn't return the list of files in that directory, where I know for sure that there are files. I am using the method FTPFile[] listFiles()
, it returns an empty array of FTPFile
.
我org.apache.commons.net.ftp.FTPClient
在我的一个应用程序中使用 FTP 服务器。我能够connect
, login
,pwd
和cwd
。但是,当我尝试访问list
这些文件时,它不会返回该目录中的文件列表,我确定其中有文件。我正在使用该方法FTPFile[] listFiles()
,它返回一个空数组FTPFile
。
Please find below the code snippet where I am trying this:
请在我尝试此操作的代码片段下方找到:
String hostname = properties.getProperty("FTP_SERVER");
String user = properties.getProperty("FTP_USER");
String passwd = properties.getProperty("FTP_PASSWD");
FTPClient client = new FTPClient();
client.connect(hostname);
client.login(user, passwd);
String reply = client.getStatus();
System.out.println(reply);
client.enterRemotePassiveMode();
client.changeWorkingDirectory("/uploads");
FTPFile[] files = client.listFiles();
System.out.println(files.length);
for (FTPFile file : files) {
System.out.println(file.getName());
}
String[] fileNames = client.listNames();
if (fileNames != null) {
for (String file : fileNames) {
System.out.println(file);
}
}
client.disconnect();
采纳答案by PapaFreud
This seems like the same issue I had (and solved), see this answer:
这似乎与我遇到(并解决)的问题相同,请参阅以下答案:
回答by Paul
Just a silly suggestion... can you do a listing on the /uploads folder using a normal FTP client. I ask this because some FTP servers are setup to not display the listing of an upload folder.
只是一个愚蠢的建议...您可以使用普通的 FTP 客户端在 /uploads 文件夹上列出清单吗?我问这个是因为某些 FTP 服务器设置为不显示上传文件夹的列表。
回答by Matthew Flaschen
First, make sure the listing works in other programs. If so, one possibility is that the file listing isn't being parsed correctly. You can try explicitly specifying the parser to use with initiateListParsing.
首先,确保列表在其他程序中有效。如果是这样,一种可能性是文件列表没有被正确解析。您可以尝试显式指定解析器使用与initiateListParsing。
回答by Shyam
After I set the mode as PASV
it is working fine now!
Thanks for all your efforts and suggestions!
在我设置模式后,PASV
它现在工作正常!感谢大家的努力和建议!
回答by Lisa
I had to same problem and it turned out to be that it couldn't parse what the server was returning for a file listing. I this line after connecting to the ftp server ftpClient.setParserFactory(new MyFTPFileEntryParserFactory());
我遇到了同样的问题,结果是它无法解析服务器为文件列表返回的内容。我在连接到 ftp 服务器之后的这一行 ftpClient.setParserFactory(new MyFTPFileEntryParserFactory());
public class MyFTPFileEntryParserFactory implements FTPFileEntryParserFactory {
private final static FTPFileEntryParser parser = new UnixFTPEntryParser() {
@Override public FTPFile parseFTPEntry(String entry) {
FTPFile ftpFile = new FTPFile();
ftpFile.setTimestamp(getCalendar(entry));
ftpFile.setSize(get(entry));
ftpFile.setName(getName(entry));
return ftpFile;
}
};
@Override public FTPFileEntryParser createFileEntryParser(FTPClientConfig config) throws ParserInitializationException {
return parser;
}
@Override public FTPFileEntryParser createFileEntryParser(String key) throws ParserInitializationException {
return parser;
}
}
}
回答by Kamil Nekanowicz
I added client.enterLocalPassiveMode()
and it works:
我添加了client.enterLocalPassiveMode()
它,它有效:
client.connect("xxx.com");
boolean login = client.login("xxx", "xxx");
client.enterLocalPassiveMode();