Download a files from FTP matching a wildcard using Java Apache Commons Net
Asked Answered
K

1

7

Basically I need to download list of matching files for the search from a FTP server. I have the code to download a specific file from a FTP server. But I need to download all the matching files with my wildcard search. How is that possible in Java?

Here is code for file downloading of a specific filename, from a FTP server -

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
public class FTPDownloadFileDemowithoutmodandfilefilter {
    public static void main(String[] args) {
        String server = "test.rebex.net";
        int port = 21;
        String user = "demo";
        String pass = "password";
        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect(server, port);
            ftpClient.login(user, pass);
            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

            File localFile = new File("C:\\project\\readme1.txt");
            FTPFile remoteFile = ftpClient.mdtmFile("/readme.txt");
            if (remoteFile != null)
            {
                OutputStream outputStream =
                    new BufferedOutputStream(new FileOutputStream(localFile));
                if (ftpClient.retrieveFile(remoteFile.getName(), outputStream))
                {
                    System.out.println("File downloaded successfully.");
                }
                outputStream.close();

                localFile.setLastModified(remoteFile.getTimestamp().getTimeInMillis());
            }

        } catch (IOException ex) {
            System.out.println("Error: " + ex.getMessage());
            ex.printStackTrace();
        } finally {
            try {
                if (ftpClient.isConnected()) {
                    ftpClient.logout();
                    ftpClient.disconnect();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

}
Klink answered 7/4, 2018 at 20:22 Comment(0)
A
5

Use FTPClient.mlistDir (recommended, if the server supports it) or FTPClient.listFiles to retrieve list of files. And then filter them according to your needs.

The following example downloads all files matching a regular expression .*\.jpg:

FTPFile[] remoteFiles = ftpClient.listFiles(remotePath);

Pattern pattern = Pattern.compile(".*\\.jpg");
Stream<FTPFile> matchingFiles =
    Arrays.stream(remoteFiles).filter(
        (FTPFile remoteFile) -> pattern.matcher(remoteFile.getName()).matches());

for (Iterator<FTPFile> iter = matchingFiles.iterator(); iter.hasNext(); ) {
    FTPFile remoteFile = iter.next();
    System.out.println("Found file " + remoteFile.getName() + ", downloading ...");

    File localFile = new File(localPath + "\\" + remoteFile.getName());
    
    OutputStream outputStream =
        new BufferedOutputStream(new FileOutputStream(localFile));
    if (ftpClient.retrieveFile(remotePath + "/" + remoteFile.getName(), outputStream))
    {
        System.out.println(
            "File " + remoteFile.getName() + " downloaded successfully.");
    }
    outputStream.close();
}
Anodize answered 9/4, 2018 at 6:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.