我正在尝试移动或重命名远程文件,而不是下载后删除远程文件,我发现可以通过出站网关move命令完成此操作,但是找不到正确的方法。
下载后请帮助重命名文件。
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public SessionFactory<LsEntry> sftpSessionFactory(
final DataloadServiceProperties DataloadServiceProperties) {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
factory.setHost(DataloadServiceProperties.getSftpHost());
factory.setPort(DataloadServiceProperties.getSftpPort());
factory.setUser(DataloadServiceProperties.getSftpUser());
if (DataloadServiceProperties.getSftpPrivateKey() != null) {
factory.setPrivateKey(DataloadServiceProperties.getSftpPrivateKey());
factory.setPrivateKeyPassphrase(
DataloadServiceProperties.getSftpPrivateKeyPassphrase());
}
else {
factory.setPassword(DataloadServiceProperties.getSftpPasword());
}
factory.setAllowUnknownKeys(true);
return new CachingSessionFactory<LsEntry>(factory);
}
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE - 1)
public SftpInboundFileSynchronizer sftpInboundFileSynchronizer(
final SessionFactory<LsEntry> sftpSessionFactory,
final DataloadServiceProperties DataloadServiceProperties) {
SftpInboundFileSynchronizer fileSynchronizer =
new SftpInboundFileSynchronizer(sftpSessionFactory);
fileSynchronizer.setDeleteRemoteFiles(false);
fileSynchronizer.setRemoteDirectory(
DataloadServiceProperties.getSftpRemoteDirectoryDownload());
fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter(
DataloadServiceProperties.getSftpRemoteDirectoryDownloadFilter()));
return fileSynchronizer;
}
在SFTP服务器中查找文件的入站通道
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE - 2)
@InboundChannelAdapter(
channel = "fromSftpChannel",
poller = @Poller(
cron = "${sftp.poller.cron}"))
public MessageSource<File> sftpMessageSource(
final SftpInboundFileSynchronizer sftpInboundFileSynchronizer,
final DataloadServiceProperties DataloadServiceProperties) {
SftpInboundFileSynchronizingMessageSource source =
new SftpInboundFileSynchronizingMessageSource(sftpInboundFileSynchronizer);
source.setLocalDirectory(
new File(DataloadServiceProperties.getSftpLocalDirectoryDownload()));
source.setAutoCreateLocalDirectory(true);
source.setLocalFilter(new AcceptOnceFileListFilter<File>());
return source;
}
下载到本地文件夹后处理文件
@Bean
@Inject
@ServiceActivator(
inputChannel = "fromSftpChannel")
public MessageHandler resultFileHandler() {
return new MessageHandler() {
@Override
public void handleMessage(final Message<?> message) throws MessagingException {
String payload = String.valueOf(message.getPayload());
if (!StringUtils.isEmpty(payload) && payload.endsWith("gz")) {
LOGGER.info("toRequest : {}", message.getPayload());
}
}
};
}
谢谢Artem Bilan,我添加了以下代码,用于在下载后将文件移至uat文件夹。现在可以正常工作了。
private static final SpelExpressionParser PARSER = new SpelExpressionParser();
@Bean(name="fromSftpChannel")
public MessageChannel fromSftpChannel() {
return new PublishSubscribeChannel();
}
@Bean
@Inject
@ServiceActivator(inputChannel = "fromSftpChannel")
@Order(Ordered.LOWEST_PRECEDENCE)
public MessageHandler moveFile() {
SftpOutboundGateway sftpOutboundGateway = new SftpOutboundGateway(sftpSessionFactory(), Command.MV.getCommand(), "'/test/'.concat(" + PARSER.parseExpression("payload.getName()").getExpressionString() + ")");
sftpOutboundGateway.setRenameExpressionString("'/test/uat/'.concat(" + PARSER.parseExpression("payload.getName()").getExpressionString() + ")");
sftpOutboundGateway.setRequiresReply(false);
sftpOutboundGateway.setOutputChannelName("nullChannel");
sftpOutboundGateway.setOrder(Ordered.LOWEST_PRECEDENCE);
sftpOutboundGateway.setAsync(true);
return sftpOutboundGateway;
}
答案 0 :(得分:3)
您需要将fromSftpChannel
设置为PublishSubscribeChannel
,并用SftpOutboundGateway
拥有第二个订阅者。您真正为Command.MV
配置的那个就够了!不要忘记配置setRenameExpression()
来指定移动的远程路径!