[英]How to upload and download a directory along with sub directory using spring integration with sftp
I need to know how to upload a directory to remote server and download a directory from remote server using sftp-spring boot with spring integration.我需要知道如何使用带有 spring 集成的 sftp-spring 引导将目录上传到远程服务器并从远程服务器下载目录。
I can upload and download the file.我可以上传和下载文件。 but I cannot able to upload a folder (entire directory).
但我无法上传文件夹(整个目录)。 I want to upload entire directory which has sub directory also and the same for download thing.
我想上传整个目录,它也有子目录,下载的东西也一样。
This is my code for download a file from remote server.这是我从远程服务器下载文件的代码。
@Bean
public DefaultSftpSessionFactory getSftpSessionFactory()
{
DefaultSftpSessionFactory defaultSftpSessionFactory=new
DefaultSftpSessionFactory();
defaultSftpSessionFactory.setHost("hostName");
defaultSftpSessionFactory.setPort(22);
defaultSftpSessionFactory.setAllowUnknownKeys(true);
defaultSftpSessionFactory.setUser("root");
defaultSftpSessionFactory.setPassword("12qwaszx");
return defaultSftpSessionFactory;
}
@Bean(name="mydefaultSync")
public SftpInboundFileSynchronizer synchronizer()
{
SftpInboundFileSynchronizer synchronizer=new
SftpInboundFileSynchronizer(getSftpSessionFactory());
synchronizer.setDeleteRemoteFiles(true);
synchronizer.setRemoteDirectory("/root/upload/");
synchronizer.setFilter(new
SftpSimplePatternFileListFilter("*.txt"));
return synchronizer;
}
@Bean(name="stfpServer")
@InboundChannelAdapter(channel="fileDownload",
poller=@Poller(fixedDelay = "3000"))
public MessageSource<File> sftpMessageSources()
{
SftpInboundFileSynchronizingMessageSource source=new SftpInboundFileSynchronizingMessageSource(synchronizer());
source.setLocalDirectory(new File("download/"));
source.setAutoCreateLocalDirectory(true);
source.setMaxFetchSize(1);
return source;
}
It works for download a file from remote server.它适用于从远程服务器下载文件。 but I need to download a directory along with sub directory from remote server using spring integration.
但我需要使用 spring 集成从远程服务器下载一个目录和子目录。
Thanks in advance...提前致谢...
The MessageSource
impls for remote files don't support recursive fetching yet: https://github.com/spring-projects/spring-integration/issues/3407 .远程文件的
MessageSource
impls 尚不支持递归获取: https://github.com/spring-projects/spring-integration/issues/3407 。
However you can use an SftpOutboundGateway
with its MGET
(or LS
) command support and RECURSIVE
option.但是,您可以使用
SftpOutboundGateway
及其MGET
(或LS
)命令支持和RECURSIVE
选项。
See more in docs: https://docs.spring.io/spring-integration/docs/current/reference/html/sftp.html#using-the-ls-command在文档中查看更多信息: https://docs.spring.io/spring-integration/docs/current/reference/html/sftp.html#using-the-ls-command
if you know this directory, for example "/home/user/myFiles/image.png" if you don't know the path you can split directories via "/" like that inputDirectory.split("/") and work with the list of directories如果您知道此目录,例如“/home/user/myFiles/image.png”,如果您不知道路径,则可以通过“/”拆分目录,例如 inputDirectory.split("/") 并使用目录列表
use these methods in ChannelSftp class:在 ChannelSftp class 中使用这些方法:
and read this tutorial: https://www.baeldung.com/java-file-sftp并阅读本教程: https://www.baeldung.com/java-file-sftp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.