title: Download a files from FTP matching a wildcard using Java Apache Commons Net
body: <p>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?</p>
  
  <p>Here is code for file downloading of a specific filename, from a FTP server - </p>
  
  <pre><code>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();
              }
          }
  
  }
  </code></pre>