简体   繁体   English

如何使用Java Config将提供文件名的文件从一个文件夹移动到远程sftp服务器上的另一个文件夹?

[英]How to move file from one folder to another folder on remote sftp server providing the filename dynamically using Java Config?

I am stuck with the problem. 我陷入了这个问题。 I have a folder om my sftp server with a file in it: folder/file.txt . 我的sftp服务器上有一个文件 ,其中有一个文件: folder / file.txt What i'm trying to do is simply to move this file to another directory : folder/subfolder/file.txt . 我想做的只是将文件移动到另一个目录: folder / subfolder / file.txt In documentation it was written that you simply need to use OutboundGateway with the MV command. 在文档中写道,您只需要通过MV命令使用OutboundGateway。 That's ok but the main problem is that i don`t know exactly what would be the name of the file, so i need to provide this name dynamically. 没关系,但是主要的问题是我不知道文件的名称是什么,所以我需要动态提供这个名称。 How can i do that? 我怎样才能做到这一点?

@Bean
@InboundChannelAdapter(value = "toSftpChannel",
        poller = @Poller(fixedDelay = "60000", maxMessagesPerPoll = "-1"))
public MessageSource<File> sftpMessageSource() {
    SftpInboundFileSynchronizingMessageSource source =
            new SftpInboundFileSynchronizingMessageSource(sftpInboundFileSynchronizer());
    source.setLocalDirectory(new File(localDirectory));
    source.setAutoCreateLocalDirectory(true);
    source.setLocalFilter(new AcceptOnceFileListFilter<File>());
    return source;
}

@Bean
public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
    SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(getSftpSessionFactory());
    fileSynchronizer.setDeleteRemoteFiles(false);
    fileSynchronizer.setRemoteDirectory("/folder");
    fileSynchronizer.setFilter(new FtpSimplePatternFileListFilter("*.txt"));
    return fileSynchronizer;
}

@Bean
@ServiceActivator(inputChannel = "toSftpChannel")
public SftpOutboundGateway moveFileHandler() {
     SftpOutboundGateway sftpOutboundGateway = new SftpOutboundGateway(getSftpSessionFactory(), "mv", "'folder/" + "file.txt" + "'");                                                  
        sftpOutboundGateway.setRenameExpression(new LiteralExpression("/folder/subfolder/" + "file.txt"));
        return sftpOutboundGateway;
    }

Instead of using literal expressions, use a dynamic expression. 与其使用文字表达式,不如使用动态表达式。

eg instead of 例如代替

"'folder/" + "file.txt" + "'"

use 采用

"'folder/' + headers['file_relativePath']"

and

sftpOutboundGateway.setRenameExpression(parser.parseExpression("'/folder/subfolder/' + headers['file_relativePath']";

(The relative path header is set up by the inbound adapter). (相对路径头由入站适配器设置)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Java 将存储在远程 SFTP 服务器中的文件复制到同一远程服务器中的另一个文件夹? - How do I copy files stored in a remote SFTP server to another folder in the same remote server using Java? PHP / Java:如何将文件从一个文件夹复制到远程服务器内的另一个文件夹 - PHP/Java: How to copy the file from one folder into another folder inside the remote server 如何使用Java将文件夹从一个位置移动到另一位置 - How to move folder from one location to another location using java Spring Integration将文件移动到另一个文件夹,然后发送到sftp服务器 - Spring Integration move file to another folder then send to sftp server 如何将FTP服务器中的CSV文件从一个文件夹移动到另一个文件夹? - How to move CSV file in FTP server one folder to another? 使用 Spring Integration 中的 Java 配置从 SFTP 复制和移动文件 - Copy and move a file from SFTP using Java config in Spring Integration 如何在 Scala 中将文件/文件从一个文件夹移动到另一个文件夹 - How to move file/files from one folder to another in Scala 将已处理文件从一个文件夹移动到 flink 中的另一个文件夹 - Move already process file from one folder to another folder in flink 如何使用Java API将文件从一个HDFS文件夹复制到另一个HDFS文件夹? - How to copy a file from one HDFS folder to another HDFS folder using Java API? 使用Java从远程服务器下载文件夹 - Downloading the folder from remote server using Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM