SftpOutboundGateway MGet 在运行时更改远程目录

问题描述 投票:0回答:1

我想尝试使用 MGet 命令从多个远程目录中提取文件,并将其存储在单独的本地目录中。但文件没有被下载,也没有抛出错误。

下面是我正在使用的示例代码。

//SFTP Config

@ServiceActivator(inputChannel = "sftpChannelMGetTest")
public MessageHandler mGetHandlerTest() {
ExpressionParser parser = new SpelExpressionParser();

        SftpOutboundGateway gateway = new SftpOutboundGateway(sftpSessionFactory(), "mget", parser.parseExpression("headers['remoteDirectory']").getExpressionString());
        gateway.setLocalDirectoryExpression(parser.parseExpression("headers['localDirectory']"));
        gateway.setFileExistsMode(FileExistsMode.REPLACE);
        gateway.setRemoteDirectoryExpression(parser.parseExpression("headers['remoteDirectory']"));
    
        return gateway;
    }
//MessagingGateway config


@Payload("new java.util.Date()")
@Gateway(requestChannel = "sftpChannelMGetTest")
List<File> getAllFilesTest(@Header("remoteDirectory") String remoteDir, @Header("localDirectory") String localDir);
//Service Call

public void getAllFilesTest() {
        List<File> allFiles = gateway.getAllFilesTest("/home/pi/sftp/patient/", "src/main/resources/files/mget/test/");
        System.out.println(allFiles);
}

如果我为一个远程目录和一个本地目录提供硬编码值,并从消息传递网关方法中删除标头选项,则可以下载文件。

spring-integration spring-integration-sftp
1个回答
0
投票

setRemoteDirectoryExpression()
无法用于
MGET
命令:

/**
 * Set the remote directory expression used to determine the remote directory to which
 * files will be sent.
 * @param remoteDirectoryExpression the remote directory expression.
 * @since 5.2
 * @see RemoteFileTemplate#setRemoteDirectoryExpression
 */
public void setRemoteDirectoryExpression(Expression remoteDirectoryExpression) {

ctor 中的表达式用于确定要执行的远程目录

MGET

请参阅有关该操作的文档:https://docs.spring.io/spring-integration/reference/sftp/outbound-gateway.html#using-the-mget-command

并注意

*
注意事项:

您使用的确定远程路径的表达式应生成以

*
结尾的结果,例如
myfiles/*
获取
myfiles
下的完整树。

所以,配置应该是这样的:

new SftpOutboundGateway(sftpSessionFactory(), "mget", "headers['remoteDirectory']");

用法是这样的:

gateway.getAllFilesTest("/home/pi/sftp/patient/*", "src/main/resources/files/mget/test/")
© www.soinside.com 2019 - 2024. All rights reserved.