英文:
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());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论