0

我使用spring integrationsftp下载和上传文件。在文档中,我发现

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

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

这是我的代码:

@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 。例如今天我下载到 20170606/ 明天我下载到 20170607 ?

编辑

这是我的选择和测试

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

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

1 回答 1

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

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

等等

表达式必须计算为表示目录绝对路径的字符串。

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

于 2017-04-06T13:03:28.170 回答