如何在春季批处理中使用sftp上传多个文件

Posted

技术标签:

【中文标题】如何在春季批处理中使用sftp上传多个文件【英文标题】:How to upload multiple files using sftp in spring batch 【发布时间】:2021-07-10 10:25:34 【问题描述】:

我有 8 个文件要在春季批处理中使用 sftp 上传到 FTP 服务器。我无法为此配置Tasklet,任何人都可以告诉我该怎么做。此外,文件名应保持与本地相同。我是 Spring 新手,所以请帮忙。

@Configuration
public class FTPSonfigurations 
    
    
    @Bean
    public DefaultSftpSessionFactory gimmeFactory()
        DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
        factory.setHost("");
        factory.setUser("");
        factory.setPassword("");
        return factory;
    
    
    @Bean
    @ServiceActivator(inputChannel = "uploadfile")
    SftpMessageHandler uploadHandler(DefaultSftpSessionFactory factory)
        SftpMessageHandler messageHandler = new SftpMessageHandler(factory);
        messageHandler.setRemoteDirectoryExpression(new LiteralExpression("/upload/ "));
        return messageHandler;
    
    


@MessagingGateway
public interface UploadMessagingGateway 

    @Gateway(requestChannel = "uploadfile")
    public void uploadFile(File file);
    


public class MyTasklet implements Tasklet 

    @Override
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception 
    
        //What to do here???
        return null;
    



【问题讨论】:

【参考方案1】:

只需将网关自动连接到 tasklet 并调用它。

@Autowired
UploadMessagingGateway gw;

...

    gw.uploadFile(file);

【讨论】:

我的目录中有 8 个文件。有什么办法可以在一个小任务中传递所有这些文件。并感谢您的帮助。 使用SFTPOutboundGatewaymput 命令;它需要一个目录或文件列表。 docs.spring.io/spring-integration/docs/current/reference/html/…

以上是关于如何在春季批处理中使用sftp上传多个文件的主要内容,如果未能解决你的问题,请参考以下文章

如何在Linux中使用sFTP上传或下载文件与文件夹

在 Linux 中使用 sFTP 上传或下载文件与文件夹

如何使用AspUpload组件上传文件

win2003系统下 FreeFileSync v8.1 如何通过sftp将需要备份的文件上传到ftp服务器进行备份。

如何使用FTP上传文件

java向SFTP服务器上传文件,如何判断服务器上的文件夹是不是存在?