使用Java Apache Commons Net从FTP下载匹配通配符的文件

7

基本上,我需要从FTP服务器下载与搜索匹配的文件列表。 我已经有了从FTP服务器下载特定文件的代码。 但是我需要使用通配符搜索下载所有匹配的文件。 在Java中如何实现?

以下是从FTP服务器下载特定文件的代码 -

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

}
1个回答

5
请使用FTPClient.mlistDir(如果服务器支持的话)或FTPClient.listFiles来检索文件列表。然后根据您的需求过滤它们。

以下示例下载与正则表达式.*\.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();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接