首页 文章

关于 spring 集成sftp的讨论

提问于
浏览
0

我使用 spring integration sftp下载和上传文件 . 在文档中,我找到了

Spring Integration通过提供三个客户端 endpoints 支持通过SFTP发送和接收文件:入站通道适配器,出站通道适配器和出站网关

当我想下载文件时,我必须分配本地目录,当我想上传文件时,我必须分配远程目录 . 但是如果我在编写代码时无法分配目录,例如我的目录与date.How关联 . 我可以在运行时分配目录吗?

这是我的代码:

@Bean
public SessionFactory<LsEntry> sftpSessionFactory(){
    DefaultSftpSessionFactory defaultSftpSessionFactory = new DefaultSftpSessionFactory();
    defaultSftpSessionFactory.setHost(host);
    defaultSftpSessionFactory.setPort(Integer.parseInt(port));
    defaultSftpSessionFactory.setUser(username);
    defaultSftpSessionFactory.setPassword(password);
    defaultSftpSessionFactory.setAllowUnknownKeys(true);
    return new CachingSessionFactory<LsEntry>(defaultSftpSessionFactory);
}

@Bean
public SftpRemoteFileTemplate sftpRemoteFileTemplate(){
    SftpRemoteFileTemplate sftpRemoteFileTemplate = new SftpRemoteFileTemplate(sftpSessionFactory());
    return sftpRemoteFileTemplate;
}

@Bean
@ServiceActivator(inputChannel = "sftpChannel")
public MessageHandler handlerGet() {
    SftpOutboundGateway sftpOutboundGateway = new SftpOutboundGateway(sftpSessionFactory(), "mget", "payload");
    sftpOutboundGateway.setLocalDirectory(new File(localDirectory));
    sftpOutboundGateway.setFilter(new SftpSimplePatternFileListFilter("*.txt"));
    sftpOutboundGateway.setSendTimeout(1000);
    return sftpOutboundGateway;
}

在messageHandler中,我必须在outboundGateway中分配localDirectory . 当我想要更改我的localDirectory几天 . 我必须将文件下载到localDirectory并移动到目标目录 . 如何在运行时分配localDirectory . 今天我下载到20170606 /明天我下载到20170607?

编辑

这是我的选择和测试

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

@Test
public void test2(){
    outboundGatewayOption.getFiles("upload/20160920/");
}

1 回答

  • 0
    sftpOutboundGateway.setLocalDirectoryExpression(
        new SpelExpressionParser().parseExpression("headers['whereToPutTheFiles']");
    

    parseExpression("@someBean.getDirectoryName(payload)")

    等等

    表达式必须求值为表示目录绝对路径的String .

    在计算表达式时,远程目录可用作变量 #remoteDirectory .

相关问题