Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URLConnection FTP list files

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'?


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 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();
}   
like image 837
KS1 Avatar asked Jan 07 '13 16:01

KS1


3 Answers

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);
like image 127
BalusC Avatar answered Nov 07 '22 17:11

BalusC


You could use Apache commons FTPClient

This would allow you to call listFiles with...

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());
        }
like image 31
david99world Avatar answered Nov 07 '22 18:11

david99world


Check out this class I found. It's does the lifting for you. Class at nsftools.com

Example would be:

FTPConnection ftpConnect = new FTPConnection();
ftpConnect.connect("ftp.example.com");
ftpConnect.login("user","pass");

System.out.println(ftpConnect.listFiles());
like image 2
C Fairweather Avatar answered Nov 07 '22 16:11

C Fairweather