Java 如何使用基于FTP的驼峰路由将所有文件从目录(包括子目录)移动到目标中没有子目录的特定目录?
由于我对apachecamel非常陌生,我坚持使用一个用例。 我想使用驼峰路由实现将所有文件移动到目标中没有子目录的特定目录 比如说- SourceDirectory/file1.xmlJava 如何使用基于FTP的驼峰路由将所有文件从目录(包括子目录)移动到目标中没有子目录的特定目录?,java,routes,ftp,apache-camel,ftp-client,Java,Routes,Ftp,Apache Camel,Ftp Client,由于我对apachecamel非常陌生,我坚持使用一个用例。 我想使用驼峰路由实现将所有文件移动到目标中没有子目录的特定目录 比如说- SourceDirectory/file1.xml SourceDirectory/subDir1/file2.xml SourceDirectory/subDir2/file3.xml SourceDirectory/subDir3/subDir4/file4.xml 应该移动到目标目录 destDir/file1.xml destDir/file2.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);
}
}
}
如果您能在路线上提供帮助,我们将不胜感激!
提前谢谢你 听起来您想要的是
展平
选项,即:
from(fromUri)
.to(destDir + "?flatten=true");
谢谢@Kalusn它成功了