FTP, FTPS and SFTP
Spring Integration allows sending and receiving files to and from a server using File Transfer Protocol (FTP), FTP Secure (FTPS), and Secure File Transfer Protocol (SFTP).
Below, Listing 22-42 and Listing 22-43 shows maven dependency required for integrating FTP and SFTP respectively using Spring Integration.
Listing 22-42. Maven dependency required for FTP integration using Spring Integration
1 2 3 4 5 6 7 8 9 |
<dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-ftp</artifactId> <version>${spring-integration-version}</version> </dependency> |
Listing 22-43. Maven dependency required for SFTP integration using Spring Integration
1 2 3 4 5 6 7 8 9 |
<dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-sftp</artifactId> <version>${spring-integration-version}</version> </dependency> |
Below, Listing 22-44 and Listing 22-45 shows declaration of Spring Integration namespace for FTP and SFTP respectively in the Spring configuration file.
Listing 22-44. Declaration of FTP namespace in Spring configuration file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration" xmlns:ftp="http://www.springframework.org/schema/integration/ftp" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.1.xsd http://www.springframework.org/schema/integration/ftp http://www.springframework.org/schema/integration/stream/spring-integration-ftp-2.1.xsd"> … </beans> |
Listing 22-45. Declaration of SFTP namespace in Spring configuration file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration" xmlns:sftp="http://www.springframework.org/schema/integration/sftp" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.1.xsd http://www.springframework.org/schema/integration/sftp http://www.springframework.org/schema/integration/stream/spring-integration-sftp-2.1.xsd"> … </beans> |
Below, Listing 22-46 and Listing 22-47 shows declaration of inbound channel adapter for FTP and SFTP respectively using Spring Integration in the Spring configuration file.
Listing 22-46. Declaration of FTP inbound channel adapters in Spring configuration file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<beans …> … <bean id="ftpClientFactory"> <property name="host" value="localhost"/> <property name="port" value="22"/> <property name="username" value="mybook"/> <property name="password" value="password"/> <property name="clientMode" value="0"/> <property name="fileType" value="2"/> <property name="bufferSize" value="100000"/> </bean> <context:property-placeholder location="/spring/ftp.properties"/> <ftp:inbound-channel-adapter id="inboundFtpChannel" channel="ftpChannel" session-factory="ftpClientFactory" filename-regex=".*\.xml$" auto-create-local-directory="true" delete-remote-files="false" remote-directory="." local-directory="file:/sample/output"> <int:poller fixed-rate="1000"/> </ftp:inbound-channel-adapter> <int:channel id="ftpChannel"> <int:queue/> </int:channel> … </beans> |
Listing 22-47. Declaration of SFTP inbound channel adapters in Spring configuration file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<beans …> … <bean id="sftpSessionFactory"> <property name="host" value="loclahost"/> <property name="privateKey" value="classpath:META-INF/keys/sftpTest"/> <property name="privateKeyPassphrase" value="springIntegration"/> <property name="port" value="22"/> <property name="user" value="mybook"/> </bean> <context:property-placeholder location="/spring/ftp.properties"/> <sftp:inbound-channel-adapter id="inboundSftpAdapter" channel="ftpChannel" session-factory="sftpSessionFactory" filename-regex=".*\.xml$" auto-create-local-directory="true" delete-remote-files="false" remote-directory="." local-directory="file:output"> <int:poller fixed-rate="1000"/> </sftp:inbound-channel-adapter> <int:channel id="ftpChannel"> <int:queue/> </int:channel> … </beans> |
Below, Listing 22-48 and Listing 22-49 shows declaration of outbound channel adapter for FTP and SFTP respectively using Spring Integration in the Spring configuration file.
Listing 22-48. Declaration of FTP outbound channel adapters in Spring configuration file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<beans …> … <bean id="ftpClientFactory"> <property name="host" value="localhost"/> <property name="port" value="22"/> <property name="username" value="mybook"/> <property name="password" value="password"/> <property name="clientMode" value="0"/> <property name="fileType" value="2"/> <property name="bufferSize" value="100000"/> </bean> <context:property-placeholder location="/spring/ftp.properties"/> <int:channel id="ftpChannel"/> <ftp:outbound-channel-adapter id="ftpOutbound" channel="ftpChannel" remote-directory="." session-factory="ftpClientFactory"/>… </beans> |
Listing 22-49. Declaration of SFTP outbound channel adapters in Spring configuration file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<beans …> … <bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory"> <property name="host" value="loclahost"/> <property name="privateKey" value="classpath:META-INF/keys/sftpTest"/> <property name="privateKeyPassphrase" value="springIntegration"/> <property name="port" value="22"/> <property name="user" value="mybook"/> </bean> <context:property-placeholder location="/spring/ftp.properties"/> <int:channel id="ftpChannel"/> <sftp:outbound-channel-adapter id="outboundSftpAdapter" channel="ftpChannel" remote-directory="." session-factory="sftpSessionFactory"/> … </beans> |
Listing 22-50. FTP/SFTP configuration file ftp.properties
1 2 3 4 5 |
host=localhost username=mybook password=password |
The only difference using FTPS is the declaration of FTPS session factory as shown in Listing 22-51 below.
Listing 22-51. FTPS session factory configured as Spring bean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<bean id="ftpClientFactory"> <property name="host" value="localhost"/> <property name="port" value="22"/> <property name="username" value="mybook"/> <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> |
Page Visitors: 13117

Tomcy John

Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023
Hi,
Thanks for this blog and information sharing!
I have one question from Spring Integration. Just thought of checking with you.
I have SimplewebserviceOutBoundGateway. I need to send a document / attachment to the webservice. How will i achieve that?
This is my current gateway configuration:
Please let me know, if you can throw some light in this area.
Thanks in Advance!
Hi,
Thank you for your information sharing.
I have one doubt,could you please suggest me the correct frameworks to address that issue.
My requirement is I would like to get and process files using spring and camel.
Thanks,
Madhu