[英]How to move file from one folder to another folder on remote sftp server providing the filename dynamically using Java Config?
I am stuck with the problem. 我陷入了这个问题。 I have a folder om my sftp server with a file in it: folder/file.txt .
我的sftp服务器上有一个文件夹 ,其中有一个文件: folder / file.txt 。 What i'm trying to do is simply to move this file to another directory : folder/subfolder/file.txt .
我想做的只是将文件移动到另一个目录: folder / subfolder / file.txt 。 In documentation it was written that you simply need to use OutboundGateway with the MV command.
在文档中写道,您只需要通过MV命令使用OutboundGateway。 That's ok but the main problem is that i don`t know exactly what would be the name of the file, so i need to provide this name dynamically.
没关系,但是主要的问题是我不知道文件的名称是什么,所以我需要动态提供这个名称。 How can i do that?
我怎样才能做到这一点?
@Bean
@InboundChannelAdapter(value = "toSftpChannel",
poller = @Poller(fixedDelay = "60000", maxMessagesPerPoll = "-1"))
public MessageSource<File> sftpMessageSource() {
SftpInboundFileSynchronizingMessageSource source =
new SftpInboundFileSynchronizingMessageSource(sftpInboundFileSynchronizer());
source.setLocalDirectory(new File(localDirectory));
source.setAutoCreateLocalDirectory(true);
source.setLocalFilter(new AcceptOnceFileListFilter<File>());
return source;
}
@Bean
public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(getSftpSessionFactory());
fileSynchronizer.setDeleteRemoteFiles(false);
fileSynchronizer.setRemoteDirectory("/folder");
fileSynchronizer.setFilter(new FtpSimplePatternFileListFilter("*.txt"));
return fileSynchronizer;
}
@Bean
@ServiceActivator(inputChannel = "toSftpChannel")
public SftpOutboundGateway moveFileHandler() {
SftpOutboundGateway sftpOutboundGateway = new SftpOutboundGateway(getSftpSessionFactory(), "mv", "'folder/" + "file.txt" + "'");
sftpOutboundGateway.setRenameExpression(new LiteralExpression("/folder/subfolder/" + "file.txt"));
return sftpOutboundGateway;
}
Instead of using literal expressions, use a dynamic expression. 与其使用文字表达式,不如使用动态表达式。
eg instead of 例如代替
"'folder/" + "file.txt" + "'"
use 采用
"'folder/' + headers['file_relativePath']"
and 和
sftpOutboundGateway.setRenameExpression(parser.parseExpression("'/folder/subfolder/' + headers['file_relativePath']";
(The relative path header is set up by the inbound adapter). (相对路径头由入站适配器设置)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.