URLConnection FTP列表文件 [英] URLConnection FTP list files

查看:153
本文介绍了URLConnection FTP列表文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 网址url =新网址(ftp:// user:pass@ftp.example.com/thefolder/); 
URLConnection connection = url.openConnection();
...
//列出文件夹中的文件...

使用像上面这样,我想知道如何抓取文件夹'thefolder'中的文件列表?






大家好,

从这个原始问题开始,我已经将这个简单的FTP连接放在一起,这个连接正在工作,
看起来不错。它可以查看/ live / conf /位置中的所有文件,并将它们全部复制到本地/ conf /位置。
唯一的问题是,它正在复制这些文件,但没有内容。它们都是0KB并且是空的!

任何人都可以看到任何明显会被复制的东西文件名,但不包含文件内容。



干杯



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();
文件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 命令(列表文件),更不用说许多其他的了。



您需要寻找支持FTP ls 命令(以及更多)的第三方库。常用的一种是 Apache Commons Net FtpClient 。在其 javadoc 中演示了如何发布 ls

  FTPClient f = new FTPClient ); 
f.connect(服务器);
f.login(username,password);
FTPFile [] files = f.listFiles(directory);


URL url =  new URL("ftp://user:pass@ftp.example.com/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'?


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!

Can anyone see anything obvious that'd be copying the filename but not file content.

Cheers

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();
}   

解决方案

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.

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列表文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆