'Download a files from FTP matching a wildcard using Java Apache Commons Net

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

}


Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1