Spring Integration:如何使用 IntegrationFlow 在 sftp 上动态创建子目录



我有一个用例,可以在动态创建的某些子目录下将文件传输到 sftp。 我使用自定义SftpMessageHandler方法和网关来工作。但是这种方法的问题是,成功上传后它不会删除本地临时文件。 为了解决这个问题,现在我正在使用 IntegrationFlow 和表达式建议(如下所示(,这确实会删除本地文件,但我不知道如何动态创建远程 subDir。我读过关于远程目录表达式的信息,但不确定如何使用/实现它。

有人解决了这个问题吗?任何帮助不胜感激!

@Bean
public IntegrationFlow sftpOutboundFlow() {
return IntegrationFlows.from("toSftpChannel")
.handle(Sftp.outboundAdapter(this.sftpSessionFactory())
.remoteFileSeparator("/")
.useTemporaryFileName(false)
.remoteDirectory("/temp"), c -> c.advice(expressionAdvice(c)))
.get();
}

@Bean
public Advice expressionAdvice(GenericEndpointSpec<FileTransferringMessageHandler<ChannelSftp.LsEntry>> c) {
ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
advice.setOnSuccessExpressionString("payload.delete()");
advice.setOnFailureExpressionString("payload + ' failed to upload'");
advice.setTrapException(true);
return advice;
}
@MessagingGateway
public interface UploadGateway {
@Gateway(requestChannel = "toSftpChannel")
void upload(File file);
}

Sftp.outboundAdapter()具有以下远程目录选项:

/**
* Specify a remote directory path.
* @param remoteDirectory the remote directory path.
* @return the current Spec
*/
public S remoteDirectory(String remoteDirectory) {
}
/**
* Specify a remote directory path SpEL expression.
* @param remoteDirectoryExpression the remote directory expression
* @return the current Spec
*/
public S remoteDirectoryExpression(String remoteDirectoryExpression) {
}
/**
* Specify a remote directory path {@link Function}.
* @param remoteDirectoryFunction the remote directory {@link Function}
* @param <P> the expected payload type.
* @return the current Spec
*/
public <P> S remoteDirectory(Function<Message<P>, String> remoteDirectoryFunction) {
}

因此,如果故事是关于动态子目录的,则可以选择一个remoteDirectoryExpressionremoteDirectory(Function),并根据应用程序上下文中的消息或某些 Bean 计算目标路径。

例如:

.remoteDirectoryExpression("'rootDir/' + headers.subDir")

另请记住,对于不存在的目录,您也需要配置一个.autoCreateDirectory(true),。

最新更新