【问题标题】:Discussion about spring integration sftp关于spring集成sftp的讨论
【发布时间】:2017-04-06 06:07:47
【问题描述】:

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

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

编辑

这是我的选择和测试

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

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

【问题讨论】:

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


    【解决方案1】:
    sftpOutboundGateway.setLocalDirectoryExpression(
        new SpelExpressionParser().parseExpression("headers['whereToPutTheFiles']");
    

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

    等等

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

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

    【讨论】:

    • 感谢您的回复。现在我将 setLocalDirectory 替换为sftpOutboundGateway.setLocalDirectoryExpression(new SpelExpressionParser().parseExpression("headers['/Users/linghu/testfile/']"));。当我运行测试下载文件时,测试通过但没有文件下载。哪里出错了。我不明白这是怎么回事。你能给我一些样品吗?
    • 你似乎误解了,如果你想要一个变量,那么你需要一个表达式,结果可以为每条消息改变。要使用headers 表达式,您将需要在网关上游设置标头,可能使用标头丰富器,在将消息发送到网关之前将myHeader 设置为'/Users/ling‌​hu/testfile/';然后使用headers['myHeader;]。您将尝试从名为/Users/ling‌​hu/testfile. 的标头中查找值。如果您使用消息传递网关将消息从 java 程序发送到网关,则可以在此处设置标头。
    • 很抱歉这么晚才回复。我找到了这个链接spring integration sftp xml sample。我用sftpOutboundGateway.setLocalDirectoryExpression(new SpelExpressionParser().parseExpression("'download'+#remoteDirectory"));设置了localDirectory。现在它可以工作了,非常感谢你的回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-02
    • 1970-01-01
    • 2015-02-11
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    相关资源
    最近更新 更多