【发布时间】:2020-11-27 15:10:03
【问题描述】:
由于我对 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);
}
}
}
在路线本身实现这一目标的任何帮助将不胜感激! 提前谢谢!
【问题讨论】:
标签: java routes ftp apache-camel ftp-client