java URLConnection FTP 列表文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14200106/
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
URLConnection FTP list files
提问by KS1
URL url = new URL("ftp://user:[email protected]/thefolder/");
URLConnection connection = url.openConnection();
...
// List files in folder...
Using something like the above, I was wondering how I could grab a list of files within folder 'thefolder'?
使用类似上面的内容,我想知道如何获取文件夹“thefolder”中的文件列表?
Hi guys,
嗨,大家好,
Following on from this original question, I have put together this simple FTP connection which is all working and looking good. It can see all files in the /live/conf/ location and copies them all to the local /conf/ location. The only issue is, it's copying the files but there's no content.They are all 0KB and empty!
继这个原始问题之后,我将这个简单的 FTP 连接放在一起,它一切正常,看起来都不错。它可以看到 /live/conf/ 位置中的所有文件并将它们全部复制到本地 /conf/ 位置。唯一的问题是,它正在复制文件但没有内容。它们都是 0KB 并且是空的!
Can anyone see anything obvious that'd be copying the filename but not file content.
任何人都可以看到任何明显的复制文件名而不是文件内容的内容。
Cheers
干杯
KPS
KPS
try {
FTPClient ftp = new FTPClient();
ftp.connect("000.000.000.000");
ftp.login("USER", "PASSWORD");
ftp.enterLocalPassiveMode();
ftp.setFileType(FTP.BINARY_FILE_TYPE);
FTPFile[] files = ftp.listFiles("/live/conf/");
for (int i=0; i < files.length; i++) {
if (files[i].getName().contains(".csv")) {
String remoteFile1 = files[i].getName();
File downloadFile1 = new File("/var/local/import/conf/"+files[i].getName());
OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
ftp.retrieveFile(remoteFile1, outputStream1);
outputStream1.close();
}
}
ftp.disconnect();
} catch (SocketException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
回答by BalusC
The Java SE URLConnection
is insuitable for the job of retrieving a list of files from a FTP host. As to FTP, it basically only supports the FTP get
or put
commands (retrieve or upload file). It does not support the FTP ls
command (list files) which you're basically looking for, let alone many others.
Java SEURLConnection
不适合从 FTP 主机检索文件列表的工作。至于FTP,基本上只支持FTPget
或put
命令(检索或上传文件)。它不支持ls
您基本上正在寻找的 FTP命令(列出文件),更不用说许多其他命令了。
You need to look for 3rd party libraries supporting the FTP ls
command (and many more). A commonly used one is the Apache Commons NetFtpClient
. In its javadocis demonstrated how to issue a ls
:
您需要寻找支持 FTPls
命令(以及更多)的第 3 方库。一个常用的是Apache Commons NetFtpClient
。在其javadoc中演示了如何发出ls
:
FTPClient f = new FTPClient();
f.connect(server);
f.login(username, password);
FTPFile[] files = f.listFiles(directory);
回答by david99world
You could use Apache commons FTPClient
This would allow you to call listFiles with...
这将允许您使用...调用 listFiles
public static void main(String[] args) throws IOException {
FTPClient client = new FTPClient();
client.connect("c64.rulez.org");
client.enterLocalPassiveMode();
client.login("anonymous", "");
FTPFile[] files = client.listFiles("/pub");
for (FTPFile file : files) {
System.out.println(file.getName());
}
回答by C Fairweather
Check out this class I found. It's does the lifting for you. Class at nsftools.com
看看我找到的这门课。它为你做举重。 在 nsftools.com 上课
Example would be:
示例是:
FTPConnection ftpConnect = new FTPConnection();
ftpConnect.connect("ftp.example.com");
ftpConnect.login("user","pass");
System.out.println(ftpConnect.listFiles());