簡體   English   中英

使用 Java Apache Commons Net 從與通配符匹配的 FTP 下載文件

[英]Download a files from FTP matching a wildcard using Java Apache Commons Net

基本上我需要從 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();
            }
        }

}

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM