一、所需jar
二、工具类
import java.net.URLConnection;
import java.util.ArrayList;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
public class FTPBulkFileDownloader {
public static void downloadFiles(String server, int port, String user, String pass, String remoteDir, String saveDir, String keyword) throws IOException {
FTPClient ftpClient = new FTPClient();
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
List filesToDownload = getFilesToDownload(ftpClient, remoteDir, keyword);
for (String file : filesToDownload) {
String remoteFilePath = remoteDir + "/" + file;
String saveFilePath = saveDir + File.separator + file;
downloadFile(ftpClient, remoteFilePath, saveFilePath);
if (ftpClient.isConnected()) {
private static List getFilesToDownload(FTPClient ftpClient, String remoteDir, String keyword) throws IOException {
List filesToDownload = new ArrayList<>();
FTPFile[] files = ftpClient.listFiles(remoteDir);
for (FTPFile file : files) {
if (file.isFile() && file.getName().contains(keyword)) {
filesToDownload.add(file.getName());
private static void downloadFile(FTPClient ftpClient, String remoteFilePath, String saveFilePath) throws IOException {
InputStream inputStream = ftpClient.retrieveFileStream(remoteFilePath);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
FileOutputStream fileOutputStream = null;
fileOutputStream = new FileOutputStream(saveFilePath);
byte[] buffer = new byte[1024];
while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, bytesRead);
System.out.println("File downloaded: " + saveFilePath);
if (bufferedInputStream != null) {
bufferedInputStream.close();
if (fileOutputStream != null) {
fileOutputStream.close();
ftpClient.completePendingCommand();
public static void main(String[] args) {
String remoteDir = "/FTP";
String saveDir = "/local";
downloadFiles(server, port, user, pass, remoteDir, saveDir, keyword);
} catch (IOException e) {