【问题标题】:Download a files from FTP matching a wildcard using Java Apache Commons Net使用 Java Apache Commons Net 从 FTP 下载匹配通配符的文件
【发布时间】:2018-09-17 14:15:26
【问题描述】:

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

}

【问题讨论】:

    标签: java ftp apache-commons-net


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2018-10-04
      • 2013-10-18
      • 1970-01-01
      • 1970-01-01
      • 2015-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多