gpt4 book ai didi

java - Spring Integration FTP 使用后删除本地文件(Spring Boot)

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:22:13 24 4
gpt4 key购买 nike

我正在尝试编写一个程序,该程序可以通过 ftp 从一台服务器获取文件并通过 ftp 将其放置在另一台服务器上。但是,我在写入本地文件后删除本地文件时遇到问题。只要它是临时的,能够在本地保存它不是问题。我尝试将 ExpressionEvaluatingRequestHandlerAdvice 与 OnSuccessExpression 一起使用,但我无法让它实际使用该表达式。代码在这里:

@Configuration
@EnableConfigurationProperties(FTPConnectionProperties.class)
public class FTPConfiguration {

private FTPConnectionProperties ftpConnectionProperties;

public FTPConfiguration(FTPConnectionProperties ftpConnectionProperties) {
this.ftpConnectionProperties = ftpConnectionProperties;
}

@Bean
public SessionFactory<FTPFile> ftpInputSessionFactory() {
DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
sf.setHost(ftpConnectionProperties.getInputServer());
sf.setUsername(ftpConnectionProperties.getInputFtpUser());
sf.setPassword(ftpConnectionProperties.getInputFtpPassword());
return new CachingSessionFactory<>(sf);
}

@Bean
public SessionFactory<FTPFile> ftpOutputSessionFactory() {
DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
sf.setHost(ftpConnectionProperties.getOutputServer());
sf.setUsername(ftpConnectionProperties.getOutputFtpUser());
sf.setPassword(ftpConnectionProperties.getOutputFtpPassword());
return new CachingSessionFactory<>(sf);
}

@Bean
public FtpInboundFileSynchronizer ftpInboundFileSynchronizer() {
FtpInboundFileSynchronizer fileSynchronizer = new FtpInboundFileSynchronizer(ftpInputSessionFactory());
fileSynchronizer.setDeleteRemoteFiles(true);
fileSynchronizer.setRemoteDirectory(ftpConnectionProperties.getInputDirectory());
fileSynchronizer.setFilter(new FtpSimplePatternFileListFilter("*.TIF"));
return fileSynchronizer;
}

@Bean
@InboundChannelAdapter(channel = "input", poller = @Poller(fixedDelay = "5000"))
public MessageSource<File> ftpMessageSource() {
FtpInboundFileSynchronizingMessageSource source = new FtpInboundFileSynchronizingMessageSource(ftpInboundFileSynchronizer());
source.setLocalDirectory(new File("ftp-inbound"));
source.setAutoCreateLocalDirectory(true);
source.setLocalFilter(new FileSystemPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), ""));
return source;
}

@Bean
@ServiceActivator(inputChannel = "input")
public MessageHandler handler() {
FtpMessageHandler handler = new FtpMessageHandler(ftpOutputSessionFactory());
handler.setRemoteDirectoryExpression(new LiteralExpression(ftpConnectionProperties.getOutputDirectory()));
handler.setFileNameGenerator(message -> {
if (message.getPayload() instanceof File) {
return ((File) message.getPayload()).getName();
} else {
throw new IllegalArgumentException("File expected as payload.");
}
});
return handler;
}

}

它完全按照预期处理远程文件,从源中删除远程文件并放入输出中,但在使用后不会删除本地文件。

最佳答案

我建议您将该 input channel 设为 PublishSubscribeChannel 并添加一个简单的订阅者:

@Bean
public PublishSubscribeChannel input() {
return new PublishSubscribeChannel();
}


@Bean
@ServiceActivator(inputChannel = "input")
public MessageHandler handler() {
...
}


@Bean
@ServiceActivator(inputChannel = "input")
public MessageHandler deleteLocalFileService() {
return m -> ((File) message.getPayload()).delete();
}

这样,带有 File 负载的相同消息将首先发送到您的 FtpMessageHandler,然后才发送到这个新的 deleteLocalFileService用于根据负载删除本地文件。

关于java - Spring Integration FTP 使用后删除本地文件(Spring Boot),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52780158/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com