简体   繁体   English

如何使用 spring 与 sftp 集成上传和下载目录及其子目录

[英]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 中使用这些方法:

  1. cd("/") method to access road directory cd("/") 方法访问道路目录
  2. mkdir("folderName") to create a new folder (surround it with try and catch because it will give an exception if the folder already exists) mkdir("folderName") 创建一个新文件夹(用 try 和 catch 包围它,因为如果该文件夹已经存在它会给出一个异常)
  3. put(InputStream src, String fileName, SftpProgressMonitor monitor, int mode) to upload the file put(InputStream src, String fileName, SftpProgressMonitor monitor, int mode) 上传文件
  4. get(fileName) to download the file get(fileName) 下载文件

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.

相关问题 如何使用sftp上传目录 - how to upload a directory with sftp 如何使用 spring 集成从 sftp 移动到本地目录时将不同的文件保存到不同的位置 - how to save different file to different location while moving from sftp to local directory using spring integration 在Spring Integration中通过sftp移动目录 - Move directory via sftp in spring integration Spring Integration 将 SFTP 远程目录设置为 null - Spring Integration is setting SFTP Remote Directory as null PipedOutput/InputStream 使用 spring 集成上传到 sftp - PipedOutput/InputStream upload to sftp using spring integration 如何在不使用 spring 引导的情况下使用 spring 集成从 sftp 服务器下载文件 - how to download file from sftp server using spring integration without using spring boot Citrus Framework-使用Spring Integration通过SFTP上传文件 - Citrus Framework - upload file via SFTP using Spring Integration 无法使用 spring 集成将文件上传到 sftp 服务器 - Not able to upload file to sftp server using spring integration 如何使用 Spring Integration 在 IntegrationFlow 链中通过 SFTP 上传文件? - How to upload file via SFTP inside an IntegrationFlow chain using Spring Integration? 如何使用spring mvc将图像上传到webapp / resources / images目录? - How to upload an image to webapp/resources/images directory using spring mvc?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM