从FTP文件夹使用Java下载特定文件(正则表达式匹配文件)。

huangapple go评论116阅读模式
英文:

Downloading specific files(regex specific files) from FTP folder using java

问题

我正在尝试使用Java从FTP文件夹下载文件,但我想要下载特定文件,而不是所有文件。如何将正则表达式添加到代码中以下载这些文件。

目前我可以从FTP位置下载所有文件。我想要添加正则表达式以便下载特定文件。

private static void downloadFolder(
    FTPClient ftpClient, String remotePath, String localPath) throws IOException
{
    System.out.println("Downloading folder " + remotePath + " to " + localPath);

    FTPFile[] remoteFiles = ftpClient.listFiles(remotePath);

    for (FTPFile remoteFile : remoteFiles)
    {
        if (!remoteFile.getName().equals(".") && !remoteFile.getName().equals(".."))
        {
            String remoteFilePath = remotePath + "/" + remoteFile.getName();
            String localFilePath = localPath + "/" + remoteFile.getName();

            if (remoteFile.isDirectory())
            {
                new File(localFilePath).mkdirs();

                downloadFolder(ftpClient, remoteFilePath, localFilePath);
            }
            else
            {
                System.out.println("Downloading file " + remoteFilePath + " to " +
                    localFilePath);

                OutputStream outputStream =
                    new BufferedOutputStream(new FileOutputStream(localFilePath));
                if (!ftpClient.retrieveFile(remoteFilePath, outputStream))
                {
                    System.out.println("Failed to download file " + remoteFilePath);
                }
                outputStream.close();
            }
        }
    }
}

希望这段代码帮助你下载特定文件。

英文:

I am trying to download files from FTP folder using java but I want to download certain files not all the files. How to add the regex to the code to download the files.

right now I am able to download all the files from FTP location. I want to add regex so it downloads specific files.

private static void downloadFolder(
    FTPClient ftpClient, String remotePath, String localPath) throws IOException
{
    System.out.println("Downloading folder " + remotePath + " to " + localPath);

    FTPFile[] remoteFiles = ftpClient.listFiles(remotePath);

    for (FTPFile remoteFile : remoteFiles)
    {
        if (!remoteFile.getName().equals(".") && !remoteFile.getName().equals(".."))
        {
            String remoteFilePath = remotePath + "/" + remoteFile.getName();
            String localFilePath = localPath + "/" + remoteFile.getName();

            if (remoteFile.isDirectory())
            {
                new File(localFilePath).mkdirs();

                downloadFolder(ftpClient, remoteFilePath, localFilePath);
            }
            else
            {
                System.out.println("Downloading file " + remoteFilePath + " to " +
                    localFilePath);

                OutputStream outputStream =
                    new BufferedOutputStream(new FileOutputStream(localFilePath));
                if (!ftpClient.retrieveFile(remoteFilePath, outputStream))
                {
                    System.out.println("Failed to download file " + remoteFilePath);
                }
                outputStream.close();
            }
        }
    }
}

答案1

得分: 1

listFiles重载,接受一个可选的FTPFileFilter。这是一个只有一个方法的接口,所以你可以使用lambda来实现。例如:

Pattern pattern = Pattern.compile("你需要的正则表达式");
FTPFile[] remoteFiles = ftpClient.listFiles(remotePath,
        file -> pattern.matcher(file.getName()).matches());
英文:

listFiles is overloaded to accept an optional FTPFileFilter. This is an interface with a single method, so you can use a lambda for the implementation. For instance:

Pattern pattern = Pattern.compile("whatever you need");
FTPFile[] remoteFiles = ftpClient.listFiles(remotePath,
        file -> pattern.matcher(file.getName()).matches());

huangapple
  • 本文由 发表于 2023年3月3日 18:25:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75625871.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定