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

FTP 会话工厂

Spring Integration 提供了可用于创建 FTP(或 FTPS)会话的工厂类。spring-doc.cadn.net.cn

默认工厂

从 3.0 版本开始,会话默认不再缓存。 请参阅 FTP 会话缓存

在配置 FTP 适配器之前,必须先配置 FTP 会话工厂。 您可以使用常规 Bean 定义来配置 FTP 会话工厂,其中实现类为 o.s.i.ftp.session.DefaultFtpSessionFactory。 以下示例展示了一个基本配置:spring-doc.cadn.net.cn

<bean id="ftpClientFactory"
    class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory">
    <property name="host" value="localhost"/>
    <property name="port" value="22"/>
    <property name="username" value="kermit"/>
    <property name="password" value="frog"/>
    <property name="clientMode" value="0"/>
    <property name="fileType" value="2"/>
    <property name="bufferSize" value="100000"/>
</bean>

对于 FTPS 连接,您可以使用 o.s.i.ftp.session.DefaultFtpsSessionFactory 代替。spring-doc.cadn.net.cn

下面的示例展示了一个完整的配置:spring-doc.cadn.net.cn

<bean id="ftpClientFactory"
    class="org.springframework.integration.ftp.session.DefaultFtpsSessionFactory">
    <property name="host" value="localhost"/>
    <property name="port" value="22"/>
    <property name="username" value="oleg"/>
    <property name="password" value="password"/>
    <property name="clientMode" value="1"/>
    <property name="fileType" value="2"/>
    <property name="useClientMode" value="true"/>
    <property name="cipherSuites" value="a,b.c"/>
    <property name="keyManager" ref="keyManager"/>
    <property name="protocol" value="SSL"/>
    <property name="trustManager" ref="trustManager"/>
    <property name="prot" value="P"/>
    <property name="needClientAuth" value="true"/>
    <property name="authValue" value="oleg"/>
    <property name="sessionCreation" value="true"/>
    <property name="protocols" value="SSL, TLS"/>
    <property name="implicit" value="true"/>
</bean>
如果您遇到连接问题,并希望跟踪会话创建以及查看哪些会话被轮询,您可以通过将日志记录器设置为 TRACE 级别(例如 log4j.category.org.springframework.integration.file=TRACE)来启用会话跟踪。

现在您只需将这些会话工厂注入到您的适配器中。 适配器使用的协议(FTP 或 FTPS)取决于已注入到该适配器的会话工厂类型。spring-doc.cadn.net.cn

为 FTP 或 FTPS 会话工厂提供值的更实用方法是使用 Spring 的属性占位符支持(参见 docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-factory-placeholderconfigurer)。