php小编小新介绍spring sftp integration是一个强大的框架,可以帮助开发者处理将文件从一个目录移动到另一个目录的任务。无论是在本地服务器还是远程服务器,这个框架都能轻松实现文件的移动和管理。它提供了一系列的api和配置选项,使开发者能够自定义移动文件的逻辑,并且能够处理各种异常情况。无论是在企业级应用程序中还是在个人项目中,spring sftp integration都是一个非常实用和值得探索的工具。
我是 spring intergation 的新手。我需要从 user1/upload 目录处理来自 sftp 服务器的文件,然后将它们移动到 user1/processed 目录。我的代码总体工作正常,但有两个问题:
当我重新启动应用程序时,目录 user1/processed 以及之前存在的所有文件都会被删除。我只想在那里写入更多文件,而不是每次都清空目录。
每次我启动应用程序时,我都会收到文件(正如我看到的它们的名称一样,我打印到控制台)我收到旧文件,它们已经被移动到已处理的目录中。这看起来真的很奇怪,因为当我通过 winscp 等其他工具连接到 sftp 时看不到这些文件。旧文件列表是否已在某处兑现?
@Value("${cielo.sftp.host}")
private String sftpHost;
@Value("${cielo.sftp.port}")
private int sftpPort;
@Value("${cielo.sftp.user}")
private String sftpUser;
@Value("${cielo.sftp.pass}")
private String sftpPasword;
@Value("${cielo.sftp.remotedir}")
private String sftpRemoteDirectoryDownload;
@Value("${cielo.sftp.localdir}")
private String sftpLocalDirectoryDownload;
@Value("${cielo.sftp.filter}")
private String sftpRemoteDirectoryDownloadFilter;
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public SessionFactory<LsEntry> sftpSessionFactory() {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
factory.setHost(sftpHost);
factory.setPort(sftpPort);
factory.setUser(sftpUser);
factory.setPassword(sftpPasword);
factory.setAllowUnknownKeys(true); //Set to true to allow connections to hosts with unknown (or changed) keys. Its default is 'false'. If false, a pre-populated knownHosts file is required.
return new CachingSessionFactory<>(factory);
}
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE - 1)
public SftpInboundFileSynchronizer sftpInboundFileSynchronizer(final SessionFactory<LsEntry> sftpSessionFactory) {
SftpInboundFileSynchronizer fileSynchronizer =
new SftpInboundFileSynchronizer(sftpSessionFactory);
fileSynchronizer.setDeleteRemoteFiles(false);
fileSynchronizer.setRemoteDirectory(sftpRemoteDirectoryDownload);
fileSynchronizer
.setFilter(new SftpSimplePatternFileListFilter(sftpRemoteDirectoryDownloadFilter)); //todo maybe use RegexPatternFileListFilter?
return fileSynchronizer;
}
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE - 2)
@InboundChannelAdapter(channel = "fromSftpChannel", poller = @Poller(fixedDelay = "1000"))
//@InboundChannelAdapter(channel = "fromSftpChannel", poller = @Poller(cron = "${cielo.sftp.poller.cron}"))
public MessageSource<File> sftpMessageSource(final SftpInboundFileSynchronizer sftpInboundFileSynchronizer) {
SftpInboundFileSynchronizingMessageSource source =
new SftpInboundFileSynchronizingMessageSource(sftpInboundFileSynchronizer);
source.setLocalDirectory(new File("/tmp/local"));
source.setAutoCreateLocalDirectory(true);
source.setLocalFilter(new AcceptOnceFileListFilter<File>());
return source;
}
@Bean
@ServiceActivator(
inputChannel = "fromSftpChannel")
public MessageHandler resultFileHandler() {
return new MessageHandler() {
@Override
public void handleMessage(final Message<?> message) throws MessagingException {
String payload = String.valueOf(message.getPayload());
System.err.println(payload);
}
};
}
private static final SpelExpressionParser PARSER = new SpelExpressionParser();
@Bean(name="fromSftpChannel")
public MessageChannel fromSftpChannel() {
return new PublishSubscribeChannel();
}
@Bean
@ServiceActivator(inputChannel = "fromSftpChannel")
@Order(Ordered.LOWEST_PRECEDENCE)
public MessageHandler moveFile() {
SftpOutboundGateway sftpOutboundGateway = new SftpOutboundGateway(sftpSessionFactory(), AbstractRemoteFileOutboundGateway.Command.MV.getCommand(), "'/user1/upload/'.concat(" + PARSER.parseExpression("payload.getName()").getExpressionString() + ")");
sftpOutboundGateway.setRenameExpressionString("'/user1/processed/'.concat(" + PARSER.parseExpression("payload.getName()").getExpressionString() + ")");
sftpOutboundGateway.setRequiresReply(false);
sftpOutboundGateway.setOutputChannelName("nullChannel");
sftpOutboundGateway.setOrder(Ordered.LOWEST_PRECEDENCE);
sftpOutboundGateway.setAsync(true);
return sftpOutboundGateway;
}感谢您的帮助!
我查看了 spring 集成中的示例 - sftp 在复制后重命名或移动远程服务器中的文件,这对我帮助很大
我还检查了官方 spring intergation sftp 文档
我不确定启动时如何清除远程目录。需要在您这边调试该行为。但我可以告诉你为什么会看到旧文件。您在处理后进行远程重命名,但这些文件的本地副本仍然存储在 source.setLocalDirectory(new File("/tmp/local")); 上。完成重命名或重新启动后,请考虑清理它。
您还可以查看 SftpStreamingMessageSource 来代替您的逻辑: https ://docs.spring.io/spring-integration/reference/sftp/streaming.html
以上就是Spring SFTP Intergation:处理将文件移动到另一个目录的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号