如何使用 FTP 上的骆驼路由将所有文件从目录(包括子目录)移动到目标中没有子目录的特定目录?
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 的新手,所以我坚持使用一个用例。
我想使用骆驼路线将所有文件移动到目标中没有子目录的特定目录,
例如-
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);
// delete empty directory
ftpClient.removeDirectory(tempSrcPath);
} else {
System.out.println("Moving "+srcFolderPath + fileName +" to = "+ targetFolder);
ftpClient.rename(srcFolderPath + fileName, targetFolder + fileName);
}
}
}
任何在路线中实现这一目标的帮助将不胜感激!
提前致谢!
听起来您正在寻找的是 flatten
选项,即:
from(fromUri)
.to(destDir + "?flatten=true");
由于我是 Apache-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);
// delete empty directory
ftpClient.removeDirectory(tempSrcPath);
} else {
System.out.println("Moving "+srcFolderPath + fileName +" to = "+ targetFolder);
ftpClient.rename(srcFolderPath + fileName, targetFolder + fileName);
}
}
}
任何在路线中实现这一目标的帮助将不胜感激! 提前致谢!
听起来您正在寻找的是 flatten
选项,即:
from(fromUri)
.to(destDir + "?flatten=true");