简体   繁体   English

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

[英]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.基本上我需要从 FTP 服务器下载匹配文件列表以进行搜索。 I have the code to download a specific file from a FTP server.我有从 FTP 服务器下载特定文件的代码。 But I need to download all the matching files with my wildcard search.但是我需要使用通配符搜索下载所有匹配的文件。 How is that possible in Java?这在Java中怎么可能?

Here is code for file downloading of a specific filename, from a FTP server -这是从 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();
            }
        }

}

Use FTPClient.mlistDir (recommended, if the server supports it) or FTPClient.listFiles to retrieve list of files.使用FTPClient.mlistDir (推荐,如果服务器支持它)或FTPClient.listFiles来检索文件列表。 And then filter them according to your needs.然后根据您的需要过滤它们。

The following example downloads all files matching a regular expression .*\\.jpg :以下示例下载与正则表达式.*\\.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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM