URLConnection FTP list files(URLConnection FTP 列表文件)
问题描述
URL url = new URL("ftp://user:pass@ftp.example.com/thefolder/");
URLConnection connection = url.openConnection();
...
// List files in folder...
使用类似上面的方法,我想知道如何获取文件夹thefolder"中的文件列表?
Using something like the above, I was wondering how I could grab a list of files within folder 'thefolder'?
从这个原始问题开始,我整理了这个简单的 FTP 连接,它一切正常,看起来不错.它可以看到/live/conf/位置的所有文件,并将它们全部复制到本地/conf/位置.
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.
唯一的问题是,它正在复制文件但没有内容.它们都是 0KB 并且是空的.
The only issue is, it is copying the files but there's no content.They are all 0KB and empty.
任何人都可以看到任何明显的复制文件名而不是文件内容的内容吗?
Can anyone see anything obvious that'd be copying the filename but not file content?
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();
}
推荐答案
Java SE URLConnection
不适合从 FTP 主机检索文件列表的工作.至于 FTP,它基本上只支持 FTP 的 get
或 put
命令(检索或上传文件).它不支持您基本上正在寻找的 FTP ls
命令(列出文件),更不用说其他许多命令了.
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.
您需要寻找支持 FTP ls
命令(以及更多)的第 3 方库.一个常用的是 Apache Commons Net FtpClient
.在其 javadoc 是演示了如何发出 ls
:
You need to look for 3rd party libraries supporting the FTP ls
command (and many more). A commonly used one is the Apache Commons Net FtpClient
. In its javadoc is demonstrated how to issue a ls
:
FTPClient f = new FTPClient();
f.connect(server);
f.login(username, password);
FTPFile[] files = f.listFiles(directory);
这篇关于URLConnection FTP 列表文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:URLConnection FTP 列表文件


基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 降序排序:Java Map 2022-01-01