Java URLConnection FTP列表文件
使用类似于上面的东西,我想知道如何获取文件夹“thefolder”中的文件列表Java URLConnection FTP列表文件,java,file,ftp,urlconnection,Java,File,Ftp,Urlconnection,使用类似于上面的东西,我想知道如何获取文件夹“thefolder”中的文件列表 从最初的问题开始,我把这个简单的FTP连接放在一起,它工作正常,看起来不错。它可以查看/live/conf/location中的所有文件,并将它们全部复制到本地/conf/location 唯一的问题是,它正在复制文件,但没有内容。它们都是0KB的空文件 有人能看到任何明显的复制文件名而不是文件内容的东西吗 URL url = new URL("ftp://user:pass@ftp.example.c
从最初的问题开始,我把这个简单的FTP连接放在一起,它工作正常,看起来不错。它可以查看/live/conf/location中的所有文件,并将它们全部复制到本地/conf/location 唯一的问题是,它正在复制文件,但没有内容。它们都是0KB的空文件 有人能看到任何明显的复制文件名而不是文件内容的东西吗
URL url = new URL("ftp://user:pass@ftp.example.com/thefolder/");
URLConnection connection = url.openConnection();
...
// List files in folder...
试试看{
FTPClient ftp=新的FTPClient();
ftp.connect(“000.000.000.000”);
ftp.login(“用户”、“密码”);
ftp.enterLocalPassiveMode();
setFileType(ftp.BINARY文件类型);
FTPFile[]files=ftp.listFiles(“/live/conf/”);
对于(int i=0;i
查看我找到的这个类。这是为你做举重。
例如:
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();
}
你可以用
这将允许您使用
FTPConnection ftpConnect = new FTPConnection();
ftpConnect.connect("ftp.example.com");
ftpConnect.login("user","pass");
System.out.println(ftpConnect.listFiles());
JavaSE不适合从FTP主机检索文件列表的工作。至于FTP,它基本上只支持FTPget
或put
命令(检索或上传文件)。它不支持FTPls
命令(列出文件),更不用说其他许多命令了
您需要寻找支持FTPls
命令的第三方库(以及更多)。一个常用的是。在its中,演示了如何发出ls
:
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());
}
有专门的FTP客户端库,你最好使用它,它会更容易。根据巴卢斯-酷,干杯,快速,简单,容易和工程!
FTPClient f = new FTPClient();
f.connect(server);
f.login(username, password);
FTPFile[] files = f.listFiles(directory);