关于弹簧集成sftp的讨论

时间:2017-04-06 06:07:47

标签: spring spring-integration spring-integration-sftp

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

  

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

当我想下载文件时,我必须分配本地目录,当我想上传文件时,我必须分配远程目录。但是如果我在编写代码时不能分配目录,例如我的目录是与date相关联。如何在运行时分配目录?

这是我的代码:

@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 :(得分:0)

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

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

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

在评估表达式时,远程目录可用作变量#remoteDirectory

相关问题