how to use SFTP Outbound Gateway 'mget' command to download files?

吃可爱长大的小学妹 提交于 2019-12-11 05:32:54

问题


I want to use the 'mget' command to download files from sftp server. here's my java config:

    @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;

}

gateway interface:

@MessagingGateway
public interface OutboundGatewayOption {
@Gateway(requestChannel = "sftpChannel")
 List<File> mget(String dir);

}

execute download:

@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;
}

}

I got this exception :

 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sftpChannel' available

I've google around but cannot found the issue ,is anyone kindly help please!


回答1:


No bean named 'sftpChannel'

Means you don't have sftpChannel MessageChannel bean yet.

I think the problem comes from the @MessagingGateway bean definition.

What you need to do is just declare that sftpChannel bean:

@Bean
public MessageChannel sftpChannel() {
    return new DirectChannel();
}

Right, in the newest Spring Integration this problem has been fixed and MessageChannel is resolved from its name lately on demand.



来源:https://stackoverflow.com/questions/46707544/how-to-use-sftp-outbound-gateway-mget-command-to-download-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!