FTP 出站网关

FTP 出站网关提供一组有限的命令来与远程 FTP 或 FTPS 服务器进行交互。 支持的命令包括:spring-doc.cadn.net.cn

使用ls命令

ls列出远程文件并支持以下选项:spring-doc.cadn.net.cn

此外,还提供了文件名过滤,其方式与inbound-channel-adapter. 请参阅 FTP 入站通道适配器spring-doc.cadn.net.cn

lsoperation 是文件名列表或FileInfo对象。 这些对象提供修改时间、权限和其他详细信息等信息。spring-doc.cadn.net.cn

远程目录ls执行 action on 的命令在file_remoteDirectory页眉。spring-doc.cadn.net.cn

当使用递归选项 (-R)、fileName包括任何 subdirectory 元素,表示文件的相对路径(相对于远程目录)。 如果-dirs选项,则每个递归目录也会作为列表中的一个元素返回。 在这种情况下,建议您不要使用-1选项,因为您无法区分文件和目录,而您可以使用FileInfo对象。spring-doc.cadn.net.cn

从版本 4.3 开始,FtpSession支持null对于list()listNames()方法。 因此,您可以省略expression属性。 为方便起见,Java 配置有两个没有expression论点。 或LS,NLST,PUTMPUT命令null被视为客户端工作目录,根据 FTP 协议。 所有其他命令都必须随expression根据请求消息评估远程路径。 您可以使用FTPClient.changeWorkingDirectory()函数,当您扩展DefaultFtpSessionFactory并实施postProcessClientAfterConnect()回调。spring-doc.cadn.net.cn

使用nlst命令

版本 5 引入了对nlst命令。spring-doc.cadn.net.cn

nlst列出远程文件名,并且仅支持一个选项:spring-doc.cadn.net.cn

nlstoperation 是文件名列表。spring-doc.cadn.net.cn

远程目录nlst执行 action on 的命令在file_remoteDirectory页眉。spring-doc.cadn.net.cn

-1选项ls命令,它使用LIST命令、nlst命令会发送一个NLST命令发送到目标 FTP 服务器。 当服务器不支持LIST(例如,由于安全限制)。 结果nlstoperation 是名称,没有其他详细信息。 因此,框架无法确定实体是否为目录,例如,无法执行筛选或递归列表。spring-doc.cadn.net.cn

使用get命令

get检索远程文件。 它支持以下选项:spring-doc.cadn.net.cn

file_remoteDirectoryheader 提供远程目录名称,而file_remoteFileheader 提供文件名。spring-doc.cadn.net.cn

get作是File对象或InputStream当您使用-stream选择。 这-stream选项允许将文件作为流检索。 对于文本文件,一个常见的用例是将此作与文件拆分器流转换器结合使用。 将远程文件作为流使用时,您负责关闭Session在流被消费后。 为方便起见,SessioncloseableResourceheader,您可以使用IntegrationMessageHeaderAccessor以下示例演示如何使用 convenience 方法:spring-doc.cadn.net.cn

Closeable closeable = new IntegrationMessageHeaderAccessor(message).getCloseableResource();
if (closeable != null) {
    closeable.close();
}

文件拆分器和流转换器等框架组件在传输数据后会自动关闭会话。spring-doc.cadn.net.cn

以下示例演示如何将文件作为流使用:spring-doc.cadn.net.cn

<int-ftp:outbound-gateway session-factory="ftpSessionFactory"
                            request-channel="inboundGetStream"
                            command="get"
                            command-options="-stream"
                            expression="payload"
                            remote-directory="ftpTarget"
                            reply-channel="stream" />

<int-file:splitter input-channel="stream" output-channel="lines" />
如果您在自定义组件中使用输入流,则必须关闭Session. 您可以在自定义代码中执行此作,也可以通过将消息的副本路由到service-activator并使用 SPEL,如下例所示:
<int:service-activator input-channel="closeSession"
    expression="headers['closeableResource'].close()" />

使用mget命令

mget根据模式检索多个远程文件,并支持以下选项:spring-doc.cadn.net.cn

mget作是List<File>object(即ListFile对象,每个对象代表一个检索到的文件)。spring-doc.cadn.net.cn

从版本 5.0 开始,如果FileExistsModeIGNORE,则输出消息的有效负载不再包含由于文件已存在而未获取的文件。 以前,该列表包含所有文件,包括已存在的文件。

用于确定远程路径的表达式应生成以- 例如somedir/将在somedir.spring-doc.cadn.net.cn

从版本 5.0 开始,递归mget,结合新的FileExistsMode.REPLACE_IF_MODIFIED模式,可用于定期在本地同步整个远程目录树。 此模式将本地文件的上次修改时间戳替换为远程时间戳,而不管-P(保留时间戳)选项。spring-doc.cadn.net.cn

使用递归 (-R)

该模式将被忽略,并被假定为。 默认情况下,将检索整个远程树。 但是,可以通过提供*FileListFilter. 树中的目录也可以通过这种方式进行筛选。 一个FileListFilter可以通过引用提供,由filename-patternfilename-regex属性。 例如filename-regex="(subDir|.*1.txt)"检索所有以1.txt在远程目录中,使用subDir子目录。 但是,下一个示例显示了一个替代方法,即 5.0 版本可用。spring-doc.cadn.net.cn

如果过滤了子目录,则不会对该子目录执行额外的遍历。spring-doc.cadn.net.cn

-dirs选项(递归的mget使用递归ls获取目录树,因此目录本身不能包含在列表中)。spring-doc.cadn.net.cn

通常,您会使用#remoteDirectory变量在local-directory-expression,以便在本地保留远程目录结构。spring-doc.cadn.net.cn

持久文件列表过滤器现在具有布尔属性forRecursion. 将此属性设置为true,还会设置alwaysAcceptDirectories,这意味着出站网关 (lsmget) 现在每次都始终遍历完整的目录树。 这是为了解决未检测到目录树深处更改的问题。 另外forRecursion=true使文件的完整路径用作元数据存储键;这解决了以下问题:如果具有相同名称的文件在不同目录中多次出现,则过滤器无法正常工作。 重要说明:这意味着对于顶级目录下的文件,将无法找到持久性元数据存储中的现有键。 因此,该属性为false默认情况下;这可能会在未来版本中更改。spring-doc.cadn.net.cn

从版本 5.0 开始,FtpSimplePatternFileListFilterFtpRegexPatternFileListFilter可以通过设置alwaysAcceptDirectoriesproperty 设置为true. 这样做允许对简单模式进行递归,如下例所示:spring-doc.cadn.net.cn

<bean id="starDotTxtFilter"
        class="org.springframework.integration.ftp.filters.FtpSimplePatternFileListFilter">
    <constructor-arg value="*.txt" />
    <property name="alwaysAcceptDirectories" value="true" />
</bean>

<bean id="dotStarDotTxtFilter"
            class="org.springframework.integration.ftp.filters.FtpRegexPatternFileListFilter">
    <constructor-arg value="^.*\.txt$" />
    <property name="alwaysAcceptDirectories" value="true" />
</bean>

定义筛选器(如前面示例中的筛选器)后,可以通过设置filter属性。spring-doc.cadn.net.cn

使用put命令

put命令将文件发送到远程服务器。 消息的有效负载可以是java.io.File一个byte[]String. 一个remote-filename-generator(或 expression) 用于命名远程文件。 其他可用属性包括remote-directory,temporary-remote-directory及其*-expression等价物:use-temporary-file-nameauto-create-directory. 有关更多信息,请参阅 schema documentation.spring-doc.cadn.net.cn

put作是String这表示传输后文件在服务器上的完整路径。spring-doc.cadn.net.cn

版本 5.2 引入了chmod属性,该属性在上传后更改远程文件权限。 您可以使用传统的 Unix 八进制格式(例如600仅允许文件所有者的读写)。 使用 java 配置适配器时,您可以使用setChmod(0600). 仅当您的 FTP 服务器支持SITE CHMOD子命令。spring-doc.cadn.net.cn

使用mput命令

mput将多个文件发送到服务器,并且仅支持一个选项:spring-doc.cadn.net.cn

  • -R:递归的。 发送目录及其子目录中的所有文件(可能已过滤)。spring-doc.cadn.net.cn

消息负载必须是java.io.File(或String) 表示本地目录。 从 5.1 版本开始,一组FileString也受支持。spring-doc.cadn.net.cn

此命令支持与put命令. 此外,本地目录中的文件可以使用以下选项之一进行过滤mput-pattern,mput-regex,mput-filtermput-filter-expression. 只要子目录本身通过过滤器,过滤器就可以使用递归。 未通过过滤器的子目录不会递归。spring-doc.cadn.net.cn

mput作是List<String>object(即List的远程文件路径)。spring-doc.cadn.net.cn

版本 5.2 引入了chmod属性,该属性允许您在上传后更改远程文件权限。 您可以使用传统的 Unix 八进制格式(例如600仅允许文件所有者的读写)。 使用 Java 配置适配器时,您可以使用setChmodOctal("600")setChmod(0600). 仅当您的 FTP 服务器支持SITE CHMOD子命令。spring-doc.cadn.net.cn

使用rm命令

rm命令删除文件。spring-doc.cadn.net.cn

rm命令中没有选项。spring-doc.cadn.net.cn

rmoperation 为Boolean.TRUE如果删除成功,或者Boolean.FALSE否则。 这file_remoteDirectoryheader 提供远程目录,而file_remoteFileheader 提供文件名。spring-doc.cadn.net.cn

使用mv命令

mv命令移动文件。spring-doc.cadn.net.cn

mv命令中没有选项。spring-doc.cadn.net.cn

expression属性定义 “from” 路径,rename-expressionattribute 定义 “to” 路径。 默认情况下,rename-expressionheaders['file_renameTo']. 此表达式的计算结果不得为 null 或空String. 如有必要,将创建任何必要的远程目录。 结果消息的有效负载为Boolean.TRUE. 这file_remoteDirectoryheader 提供原始远程目录,而file_remoteFileheader 提供文件名。 新路径位于file_renameTo页眉。spring-doc.cadn.net.cn

从版本 5.5.6 开始,remoteDirectoryExpression可用于mv命令。 如果 “from” 文件不是完整的文件路径,则remoteDirectoryExpression用作远程目录。 这同样适用于“to”文件,例如,如果任务只是重命名某个目录中的远程文件。spring-doc.cadn.net.cn

有关 FTP 出站网关命令的其他信息

getmget命令支持local-filename-generator-expression属性。 它定义了一个 SPEL 表达式,用于在传输过程中生成本地文件的名称。 评估上下文的根对象是请求消息。 这remoteFileName变量,这对于mget,也可用 — 例如local-filename-generator-expression="#remoteFileName.toUpperCase() + headers.something".spring-doc.cadn.net.cn

getmget命令支持local-directory-expression属性。 它定义了一个 SPEL 表达式,用于在传输过程中生成本地目录的名称。 评估上下文的根对象是请求消息 but. 这remoteDirectory变量,这对于mget,也可用 — 例如:local-directory-expression="'/tmp/local/' + #remoteDirectory.toUpperCase() + headers.something". 此属性与local-directory属性。spring-doc.cadn.net.cn

对于所有命令,网关的 'expression' 属性提供命令作的路径。 对于mget命令,表达式的计算结果可能为 '',表示检索所有文件,或者 'somedirectory/',依此类推。spring-doc.cadn.net.cn

以下示例显示了为ls命令:spring-doc.cadn.net.cn

<int-ftp:outbound-gateway id="gateway1"
    session-factory="ftpSessionFactory"
    request-channel="inbound1"
    command="ls"
    command-options="-1"
    expression="payload"
    reply-channel="toSplitter"/>

发送到toSplitterchannel 是String对象,每个对象都包含一个文件名。 如果command-options属性,则FileInfo对象。 它使用以空格分隔的选项 — 例如command-options="-1 -dirs -links".spring-doc.cadn.net.cn

从版本 4.2 开始,GET,MGET,PUTMPUT命令支持FileExistsMode属性 (mode当使用命名空间支持时)。 这会影响本地文件存在 (GETMGET) 或远程文件存在 (PUTMPUT). 支持的模式包括REPLACE,APPEND,FAILIGNORE. 为了向后兼容,默认PUTMPUToperations 为REPLACE. 为GETMGET作,默认值为FAIL.spring-doc.cadn.net.cn

从版本 5.0 开始,setWorkingDirExpression() (working-dir-expressionin XML) 选项。FtpOutboundGateway (<int-ftp:outbound-gateway>在 XML 中)。 它允许您在运行时更改客户端工作目录。 根据请求消息计算表达式。 每次网关作后,都会恢复以前的工作目录。spring-doc.cadn.net.cn

使用 Java 配置进行配置

Spring 以下 Boot 应用程序显示了如何使用 Java 配置配置出站网关的示例:spring-doc.cadn.net.cn

@SpringBootApplication
public class FtpJavaApplication {

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

    @Bean
    public SessionFactory<FTPFile> ftpSessionFactory() {
        DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
        sf.setHost("localhost");
        sf.setPort(port);
        sf.setUsername("foo");
        sf.setPassword("foo");
        sf.setTestSession(true);
        return new CachingSessionFactory<FTPFile>(sf);
    }

    @Bean
    @ServiceActivator(inputChannel = "ftpChannel")
    public MessageHandler handler() {
        FtpOutboundGateway ftpOutboundGateway =
                          new FtpOutboundGateway(ftpSessionFactory(), "ls", "'my_remote_dir/'");
        ftpOutboundGateway.setOutputChannelName("lsReplyChannel");
        return ftpOutboundGateway;
    }

}

使用 Java DSL 进行配置

Spring 下面的 Boot 应用程序显示了如何使用 Java DSL 配置出站网关的示例:spring-doc.cadn.net.cn

@SpringBootApplication
public class FtpJavaApplication {

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

    @Bean
    public SessionFactory<FTPFile> ftpSessionFactory() {
        DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
        sf.setHost("localhost");
        sf.setPort(port);
        sf.setUsername("foo");
        sf.setPassword("foo");
        sf.setTestSession(true);
        return new CachingSessionFactory<FTPFile>(sf);
    }

    @Bean
    public FtpOutboundGatewaySpec ftpOutboundGateway() {
        return Ftp.outboundGateway(ftpSessionFactory(),
            AbstractRemoteFileOutboundGateway.Command.MGET, "payload")
            .options(AbstractRemoteFileOutboundGateway.Option.RECURSIVE)
            .regexFileNameFilter("(subFtpSource|.*1.txt)")
            .localDirectoryExpression("'localDirectory/' + #remoteDirectory")
            .localFilenameExpression("#remoteFileName.replaceFirst('ftpSource', 'localTarget')");
    }

    @Bean
    public IntegrationFlow ftpMGetFlow(AbstractRemoteFileOutboundGateway<FTPFile> ftpOutboundGateway) {
        return f -> f
            .handle(ftpOutboundGateway)
            .channel(c -> c.queue("remoteFileOutputChannel"));
    }

}

出站网关部分成功 (mgetmput)

当您对多个文件执行作时(通过使用mgetmput),则在传输一个或多个文件后的某个时间可能会发生异常。 在这种情况下(从版本 4.2 开始),一个PartialSuccessException被抛出。 和往常一样MessagingException属性 (failedMessagecause),则此异常具有两个附加属性:spring-doc.cadn.net.cn

这些属性允许您确定哪些文件已成功传输,哪些文件未成功传输。spring-doc.cadn.net.cn

对于递归mputPartialSuccessException可能已嵌套PartialSuccessException事件。spring-doc.cadn.net.cn

请考虑以下目录结构:spring-doc.cadn.net.cn

root/
|- file1.txt
|- subdir/
   | - file2.txt
   | - file3.txt
|- zoo.txt

如果异常发生在file3.txtPartialSuccessException由 gateway 抛出的derivedInputfile1.txt,subdirzoo.txtpartialResultsfile1.txt. 其cause是另一个PartialSuccessExceptionderivedInputfile2.txtfile3.txtpartialResultsfile2.txt.spring-doc.cadn.net.cn