![](/img/trans.png)
[英]Spring Integration Java DSL - Http Outbound Gateway uri variable Expression
[英]Moving file/files from remote directory to another Spring Integration SFTP Outbound Gateway Java Config/Java DSL
在我的项目中,我能够列出文件、获取它们、处理它们,现在我需要将远程文件移动到一个新目录,但是我在使用 mv 命令时遇到了问题。 我以此为参考。
我的代码
@MessagingGateway
public interface SftpMessagingGateway {
@Gateway(requestChannel = "listFiles")
List<File> readListOfFiles(String payload);
@Gateway(requestChannel = "getFiles")
List<File> getFiles(String payload);
@Gateway(requestChannel = "moveFiles")
void backupFiles(List<File> payload);
}
@Bean
public SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory() {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
factory.setHost(host);
factory.setPort(22);
factory.setUser(username);
factory.setPassword(password);
factory.setAllowUnknownKeys(true);
return new CachingSessionFactory<>(factory);
}
@Bean
@ServiceActivator(inputChannel = "listFiles")
public MessageHandler sftpReadHandler(){
return new SftpOutboundGateway(sftpSessionFactory(), "ls", "'/oldDirectory/'");
}
@Bean
@ServiceActivator(inputChannel = "moveFiles")
public MessageHandler sftpPutHandler(){
SftpOutboundGateway sftpOutboundGateway = new SftpOutboundGateway(sftpSessionFactory(), "mv", "'/oldDirectory/'"+ "+ headers['file_relativePath']" );
sftpOutboundGateway.setRenameExpression(new SpelExpressionParser().parseExpression("'/newDirectory/'"+ "+ headers['file_relativePath']"));
return sftpOutboundGateway;
}
@Bean
@ServiceActivator(inputChannel = "getFiles")
public MessageHandler sftpGetHandler(){
SftpOutboundGateway sftpOutboundGateway = new SftpOutboundGateway(sftpSessionFactory(), "mget", "'/oldDirectory/'");
sftpOutboundGateway.setLocalDirectory(new File("sftp-inbound"));
sftpOutboundGateway.setAutoCreateLocalDirectory(true);
return sftpOutboundGateway;
}
我在服务中调用它: sftpGateway.backupFiles(files)
我想将所有已处理的文件从“'/oldDirectory/'”移动到“'/newDirectory/'” 我收到一个错误: "error occurred in message handler [bean 'sftpPutHandler'; defined in: 'class path resource [SFTPConfig.class]'; from source: 'org.springframework.core.type.classreading.SimpleMethodMetadata@1654a892']; nested exception is org.springframework.messaging.MessagingException: Failed to execute on session; nested exception is org.springframework.core.NestedIOException: Failed to delete file /newDirectory/null; nested exception is 2: No such file"
我已经尝试更改有效负载和重命名表达式,但我认为我无法在 oldDirectory 中的每个文件上运行此命令或一次将所有文件从旧目录移动到新目录。
你的问题是你的void backupFiles(List<File> payload);
您不满足 SFTP mv 期望的网关合同。 您有几个基于headers['file_relativePath']
,但是当网关方法调用发送消息时,没有这样的 header 。
但是我仍然不知道这是如何工作的:您发送文件列表并且在调用 SFTP mv 之前不拆分该列表。
或者使用splitter
处理 SFTP 网关上的单个文件。 或者将@Gateway
合约作为单个文件并在列表迭代器中调用它。
您可能只需将oldDirectory
重命名为newDirectory
,并分别重新定位所有远程内容。 但我不确定这是否是你想要实现的......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.