如需使用最新稳定版本,请使用 Spring Integration 7.0.4spring-doc.cadn.net.cn

SFTP 出站通道适配器

SFTP 出站通道适配器是一种特殊的MessageHandler,它连接到远程目录,并为每个作为传入Message负载接收的文件发起文件传输。 它还支持多种文件表示形式,因此您不必局限于File对象。 与 FTP 出站适配器类似,SFTP 出站通道适配器支持以下负载:spring-doc.cadn.net.cn

以下示例展示了如何配置 SFTP 出站通道适配器:spring-doc.cadn.net.cn

<int-sftp:outbound-channel-adapter id="sftpOutboundAdapter"
    session-factory="sftpSessionFactory"
    channel="inputChannel"
    charset="UTF-8"
    remote-file-separator="/"
    remote-directory="foo/bar"
    remote-filename-generator-expression="payload.getName() + '-mysuffix'"
    filename-generator="fileNameGenerator"
    use-temporary-filename="true"
    chmod="600"
    mode="REPLACE"/>

有关这些属性的更多详细信息,请参阅模式spring-doc.cadn.net.cn

SpEL 与 SFTP 出站适配器

与 Spring Integration 中的许多其他组件一样,在配置 SFTP 出站通道适配器时,您可以使用 Spring 表达式语言 (SpEL),只需指定两个属性:remote-directory-expressionremote-filename-generator-expression前文已描述)。 表达式求值上下文以消息为根对象,这使得您能够使用表达式,根据消息中的数据(来自 'payload' 或 'headers')动态计算文件名或现有目录路径。 在前面的示例中,我们定义了 remote-filename-generator-expression 属性,其表达式值基于原始名称计算文件名,同时附加后缀:'-mysuffix'。spring-doc.cadn.net.cn

从版本 4.1 开始,在传输文件时您可以指定 mode。 默认情况下,现有文件会被覆盖。 这些模式由 FileExistsMode 枚举定义,其中包括以下值:spring-doc.cadn.net.cn

使用 IGNOREFAIL 时,文件不会被传输。 FAIL 会导致抛出异常,而 IGNORE 会静默忽略传输(尽管会产生一条 DEBUG 日志条目)。spring-doc.cadn.net.cn

版本 4.3 引入了 chmod 属性,您可以在上传后使用它来更改远程文件的权限。 您可以使用传统的 Unix 八进制格式(例如,600 仅允许文件所有者进行读写)。 在使用 Java 配置适配器时,可以使用 setChmodOctal("600")setChmod(0600)spring-doc.cadn.net.cn

避免写入部分文件

在处理文件传输时,一个常见问题是可能处理了不完整的文件。 文件可能在传输尚未实际完成时就出现在文件系统中。spring-doc.cadn.net.cn

为了解决这个问题,Spring Integration SFTP 适配器使用一种通用算法:文件先以临时名称传输,待完全传输完成后再进行重命名。spring-doc.cadn.net.cn

默认情况下,正在传输的每个文件在文件系统中都会显示一个额外的后缀,默认情况下为 .writing。 您可以通过设置 temporary-file-suffix 属性来更改它。spring-doc.cadn.net.cn

然而,在某些情况下您可能不希望使用此技术(例如,如果服务器不允许重命名文件)。 对于此类情况,您可以通过将 use-temporary-file-name 设置为 false 来禁用此功能(默认值为 true)。 当该属性为 false 时,文件将以最终名称写入,消费应用程序需要其他机制来检测在访问文件之前文件是否已完全上传。spring-doc.cadn.net.cn

使用 Java 配置进行配置

以下 Spring Boot 应用程序展示了如何使用 Java 配置出站适配器:spring-doc.cadn.net.cn

@SpringBootApplication
@IntegrationComponentScan
public class SftpJavaApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context =
                    new SpringApplicationBuilder(SftpJavaApplication.class)
                        .web(false)
                        .run(args);
        MyGateway gateway = context.getBean(MyGateway.class);
        gateway.sendToSftp(new File("/foo/bar.txt"));
    }

    @Bean
    public SessionFactory<SftpClient.DirEntry> sftpSessionFactory() {
        DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
        factory.setHost("localhost");
        factory.setPort(port);
        factory.setUser("foo");
        factory.setPassword("foo");
        factory.setAllowUnknownKeys(true);
        factory.setTestSession(true);
        return new CachingSessionFactory<SftpClient.DirEntry>(factory);
    }

    @Bean
    @ServiceActivator(inputChannel = "toSftpChannel")
    public MessageHandler handler() {
        SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory());
        handler.setRemoteDirectoryExpressionString("headers['remote-target-dir']");
        handler.setFileNameGenerator(new FileNameGenerator() {

            @Override
            public String generateFileName(Message<?> message) {
                 return "handlerContent.test";
            }

        });
        return handler;
    }

    @MessagingGateway
    public interface MyGateway {

         @Gateway(requestChannel = "toSftpChannel")
         void sendToSftp(File file);

    }
}

使用 Java DSL 进行配置

以下 Spring Boot 应用程序展示了如何使用 Java DSL 配置出站适配器:spring-doc.cadn.net.cn

@SpringBootApplication
public class SftpJavaApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(SftpJavaApplication.class)
            .web(false)
            .run(args);
    }

    @Bean
    public IntegrationFlow sftpOutboundFlow() {
        return IntegrationFlow.from("toSftpChannel")
            .handle(Sftp.outboundAdapter(this.sftpSessionFactory, FileExistsMode.FAIL)
                         .useTemporaryFileName(false)
                         .remoteDirectory("/foo")
            ).get();
    }

}