英文:
How to move all files from a directory(including subdirectories) to a specific directory without subdirectories in target using camel route over FTP?
问题
由于我对Apache Camel非常陌生,我被困在一个使用案例中。我想要通过Camel路由将所有文件移动到一个特定的目录,而不带子目录,例如:
SourceDirectory/file1.xml
SourceDirectory/subDir1/file2.xml
SourceDirectory/subDir2/file3.xml
SourceDirectory/subDir3/subDir4/file4.xml
应该移动到一个目标目录:
destDir/file1.xml
destDir/file2.xml
destDir/file3.xml
destDir/file4.xml
以下代码将包括所有子目录的文件复制到目标目录中:
String src = "ftp://username:password@host/srcDir/";
String destDir = "ftp://username:password@host/destDir/";
fromUri = src + "?recursive=true&delete=true";
from(fromUri)
.to(destDir);
为了在路由中实现这个目标,目前我正在使用FTP客户端。以下是移动文件的私有方法:
private void moveOverFTP(String from, String to) {
FTPClient ftpClient = new FTPClient();
try {
URL url = new URL(from);
String[] info = url.getUserInfo().split(":");
ftpClient.connect(url.getHost());
ftpClient.login(info[0], info[1]);
String srcFolderPath = url.getPath();
String targetFolder = new URL(to).getPath();
move(srcFolderPath, targetFolder, ftpClient);
ftpClient.logout();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
private void move(String srcFolderPath, String targetFolder, FTPClient ftpClient) throws IOException {
FTPFile[] files = ftpClient.listFiles(srcFolderPath);
for (FTPFile file : files) {
String fileName = file.getName();
if (file.isDirectory()) {
String tempSrcPath = srcFolderPath + fileName + "/";
move(tempSrcPath, targetFolder, ftpClient);
// 删除空目录
ftpClient.removeDirectory(tempSrcPath);
} else {
System.out.println("Moving " + srcFolderPath + fileName + " to " + targetFolder);
ftpClient.rename(srcFolderPath + fileName, targetFolder + fileName);
}
}
}
如果能在路由中实现这个目标,将不胜感激!提前感谢您的帮助!
英文:
As I am very new to Apache-Camel, I stuck with a use case.
I want to achieve moving of all files to a specific directory without sub-directories in target using camel route,
for example-
SourceDirectory/file1.xml
SourceDirectory/subDir1/file2.xml
SourceDirectory/subDir2/file3.xml
SourceDirectory/subDir3/subDir4/file4.xml
should be moved to a destination Directory
destDir/file1.xml
destDir/file2.xml
destDir/file3.xml
destDir/file4.xml
The code below copies file including all sub-directories to destination
String src="ftp://username:password@host/srcDir/";
String destDir="ftp://username:password@host/destDir/";
fromUri = src+"?recursive=true&delete=true";
from(fromUri)
.to(destDir);
To achieve this currently I am using ftp client
private void moveOverFTP(String from, String to) {
FTPClient ftpClient = new FTPClient();
try {
URL url = new URL(from);
String[] info = url.getUserInfo().split(":");
ftpClient.connect(url.getHost());
ftpClient.login(info[0], info[1]);
String srcFolderPath = url.getPath();
String targetFolder = new URL(to).getPath();
move(srcFolderPath, targetFolder, ftpClient);
ftpClient.logout();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
private void move(String srcFolderPath, String targetFolder, FTPClient ftpClient) throws IOException {
FTPFile[] files = ftpClient.listFiles(srcFolderPath);
for (FTPFile file : files) {
String fileName = file.getName();
if (file.isDirectory()) {
String tempSrcPath = srcFolderPath + fileName + "/";
move(tempSrcPath, targetFolder, ftpClient);
// delete empty directory
ftpClient.removeDirectory(tempSrcPath);
} else {
System.out.println("Moving "+srcFolderPath + fileName +" to = "+ targetFolder);
ftpClient.rename(srcFolderPath + fileName, targetFolder + fileName);
}
}
}
Any help to achieve this in route itself would be appreciated!
Thank you in advance!
答案1
得分: 1
你似乎正在寻找的是flatten
选项,即:
from(fromUri)
.to(destDir + "?flatten=true");
英文:
Sounds like what you are looking for is the flatten
option, ie.:
from(fromUri)
.to(destDir + "?flatten=true");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论