我想使用'mget'命令从sftp服务器下载文件。这是我的java配置:
@Bean
public SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory() {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
....
return new CachingSessionFactory<>(factory);
}
@Bean(name = "lsGateway")
@ServiceActivator(inputChannel = "sftpChannel")
public MessageHandler handlerLs() {
// call 'mget' command to download all the files in server folder
SftpOutboundGateway sftpOutboundGateway = new SftpOutboundGateway(sftpSessionFactory(), "mget", "payload");
sftpOutboundGateway.setLocalDirectory(new File("/local/path"));
return sftpOutboundGateway;
}
网关接口:
@MessagingGateway
public interface OutboundGatewayOption {
@Gateway(requestChannel = "sftpChannel")
List<File> mget(String dir);
}
执行下载:
@Component
public class Step1Tasklet implements Tasklet {
@Autowired
private OutboundGatewayOption gatewayOption;
@Override
public RepeatStatus execute(StepContribution stepContribution,
ChunkContext chunkContext) throws Exception {
// download files in server folder
List<File> files = gatewayOption.mget("/ftp/server/path/");
return RepeatStatus.FINISHED;
}
}
我遇到了这个例外:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sftpChannel' available
我已经google了但是找不到问题,请有人帮忙!
答案 0 :(得分:0)
没有名为'sftpChannel'的bean
表示您还没有sftpChannel
MessageChannel
bean。
我认为问题来自@MessagingGateway
bean定义。
您需要做的就是声明sftpChannel
bean:
@Bean
public MessageChannel sftpChannel() {
return new DirectChannel();
}
是的,在最新的Spring Integration中,此问题已修复,MessageChannel
最近根据需要从其名称中解析。