Java
Chilkat2 Java
Download Multiple Files Matching Pattern
See more FTP Examples
The MGetFiles method can be called to download all files matching a wildcarded filename pattern.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Ftp2;
public class ChilkatExample {
public static void main(String[] args) {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Ftp2 ftp = new Ftp2();
ftp.setHostname("ftp.example.com");
ftp.setUsername("myUsername");
ftp.setPassword("myPassword");
ftp.setPort(21);
ftp.setAuthTls(true);
// Connect and login to the FTP server.
try {
ftp.connect();
// Change to the remote directory where the files are located.
// This step is only necessary if the files are not in the root directory
// of the FTP account.
ftp.changeRemoteDir("qa");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Download all files with filenames matching "*.txt";
// The files are downloaded into c:/temp/qa_output
int numFilesDownloaded = ftp.mGetFiles("*.txt", "c:/temp/qa_output");
if (numFilesDownloaded < 0) {
System.out.println(ftp.getLastErrorText());
return;
}
ftp.disconnect();
System.out.println(numFilesDownloaded + " Files Downloaded!");
}
}