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

9fkzdhlc  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(369)

我有8个文件,我想上传到一个ftp服务器使用sftp在 Spring 批处理。我无法配置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;
    }

}
wfveoks0

wfveoks01#

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

@Autowired
UploadMessagingGateway gw;

...

    gw.uploadFile(file);

相关问题