![](/img/trans.png)
[英]How to move file from directory to another with spring integration and sftp
[英]How to create a method to move file from one directory to another using Spring Integration File Support?
我正在自学Spring。 我想创建一个方法,其参数将是源目录,目标目录和文件名。
boolean moveFile(String sourceDir, String targetDir, String fileName)
该方法可以将文件从源目录移动到目标目录。 我已经阅读了Spring集成文件支持文档 。 我也用谷歌搜索示例,但是总是得到示例,其中两个目录都在xml文件中进行了硬编码,并且将监视源目录,并在新文件出现时将文件移动到目标目录。 如何创建我要完成的方法。
谢谢。
嗯,Spring Integration不提供这样的功能,因为它实际上作为Java中的单个方法存在。 请参阅java.nio.file.Files
: https : java.nio.file.Files
。 nio.file.Path,%20java.nio.file.CopyOption ...)
将文件移动或重命名为目标文件。
以下是一些示例和说明:您可以在此处找到一些信息: http : //tutorials.jenkov.com/java-nio/files.html
如果您仍在寻找一些Spring Integration的即用型解决方案,则在有效负载为File
, FileWritingMessageHandler
提供一些功能可以从一个文件复制到另一个File
:
<int-file:outbound-channel-adapter id="moveFile"
directory="/destinationDir"
delete-source-files="true"
filename-generator-expression="'foo.txt'" />
这样FileWritingMessageHandler
执行以下逻辑:
if (!FileExistsMode.APPEND.equals(this.fileExistsMode) && this.deleteSourceFiles) {
rename(sourceFile, resultFile);
return resultFile;
}
其中rename()
正是这样:
private static void rename(File source, File target) throws IOException {
Files.move(source.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.