{"id":2389,"date":"2010-07-21T15:07:50","date_gmt":"2010-07-21T13:07:50","guid":{"rendered":"http:\/\/blog.jteam.nl\/?p=2389"},"modified":"2010-07-21T15:07:50","modified_gmt":"2010-07-21T13:07:50","slug":"connecting-to-ftp-server-with-spring-integration","status":"publish","type":"post","link":"https:\/\/trifork.nl\/blog\/connecting-to-ftp-server-with-spring-integration\/","title":{"rendered":"Connecting to FTP server with Spring Integration"},"content":{"rendered":"<p>For one of our project I needed to read zip files from a FTP server and import the content in a system. In this post I will explain how I have used the Spring Integration to connect with a FTP server and retrieve Zip files.<br \/>\n<!--more--><\/p>\n<h2>FTP Client Factory<\/h2>\n<p>As the FTP extension for Spring Integration has no official release yet, I have used the latest build which can be found at the <a href=\"http:\/\/www.springsource.org\/extensions\/se-sia\" target=\"_blank\" rel=\"noopener\">Spring Integration Adapters site<\/a>.<\/p>\n<p>The extension provides a client factory that allows you to connect with a client. The class I have used is the <a href=\"https:\/\/src.springframework.org\/svn\/se-sia\/trunk\/org.springframework.integration.ftp\/src\/main\/java\/org\/springframework\/integration\/ftp\/DefaultFTPClientFactory.java\" target=\"_blank\" rel=\"noopener\">DefaultFTPClientFactory<\/a> which implements the interface <a href=\"https:\/\/src.springframework.org\/svn\/se-sia\/trunk\/org.springframework.integration.ftp\/src\/main\/java\/org\/springframework\/integration\/ftp\/FTPClientFactory.java\" target=\"_blank\" rel=\"noopener\">FTPClientFactory<\/a>.<\/p>\n<p>When you have copied these files to your own project, you can configure a client by using the following code:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n    &lt;bean id=&quot;defaultClient&quot; class=&quot;nl.jteam.importer.ftp.DefaultFTPClientFactory&quot;&gt;\n        &lt;property value=&quot;${ftp.remotedir}&quot; name=&quot;remoteWorkingDirectory&quot;&gt;&lt;\/property&gt;\n        &lt;property value=&quot;${ftp.username}&quot; name=&quot;username&quot;&gt;&lt;\/property&gt;\n        &lt;property value=&quot;${ftp.password}&quot; name=&quot;password&quot;&gt;&lt;\/property&gt;\n        &lt;property value=&quot;${ftp.port}&quot; name=&quot;port&quot;&gt;&lt;\/property&gt;\n        &lt;property value=&quot;${ftp.host}&quot; name=&quot;host&quot;&gt;&lt;\/property&gt;\n    &lt;\/bean&gt;\n<\/pre>\n<h2>Reading the files<\/h2>\n<p>Now that the client is configured, we can read the files from the FTP server. In the method <em>getFilesFromFTPClient()<\/em> we get a <a href=\"http:\/\/commons.apache.org\/net\/api\/org\/apache\/commons\/net\/ftp\/FTPClient.html\" target=\"_blank\" rel=\"noopener\">FTPClient<\/a> by calling the <em>getClient()<\/em> method on the client factory. The client API provides you with the possibility to retrieve, delete, rename or store files. The API offers a lot more, but I won&#8217;t discuss all the methods here. In our case we wanted to retrieve only zip files. As the client does not provide you the functionality to retrieve files from a specific extension, you have to do it yourself by, for example, checking the extension of each file.<\/p>\n<p>Don\u2019t forget to close the connection with the FTP client once you are done with handling files.<\/p>\n<p>Because we want use Spring Integration to send the files to the class that handles the zip files, we create a <a href=\"http:\/\/static.springsource.org\/spring-integration\/reference\/htmlsingle\/spring-integration-reference.html#overview-components\" target=\"_blank\" rel=\"noopener\">Message<\/a> with the list of zip files.<\/p>\n<p>The (partial) code of the FTPInboundAdapter class:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\n    public FTPInboundAdapter(FTPClientFactory clientFactory, String localTmpDirName) throws IOException {\n        Assert.notNull(localTmpDirName, &quot;The directory name to write the files to can not be null&quot;);\n        this.clientFactory = clientFactory;\n        localDirectory = ImportUtils.ensureTempDirExists(localTmpDirName);\n    }\n\n    public Message&lt;list&gt;&lt;file&gt;&amp;gt; getFilesFromFTPClient() {\n        FTPClient client = null;\n        try {\n            client = clientFactory.getClient();\n\n            List&lt;file&gt; localZipFiles = retrieveRemoteZipFiles(client);\n\n            return MessageBuilder.withPayload(localZipFiles).build();\n        } catch (IOException e) {\n            throw new MessagingException(&quot;Problem occurred while trying to retrieve files.&quot;, e);\n        } finally {\n            closeFtpClient(client);\n        }\n    }\n\n    private void closeFtpClient(FTPClient client) {\n        if (client != null &amp;amp;&amp;amp; client.isConnected()) {\n            try {\n                client.disconnect();\n            } catch (IOException e) {\n                logger.warn(&quot;Error occurred when disconnection FTP client&quot;, e);\n            }\n        }\n    }\n<\/pre>\n<p>The configuration code for the adapter:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n    &lt;bean id=&quot;ftpInboundAdapter&quot; class=&quot;nl.jteam.importer.ftp.FTPInboundAdapter&quot;&gt;\n        &lt;constructor-arg ref=&quot;defaultClient&quot; \/&gt;\n        &lt;constructor-arg value=&quot;someLocalDirectory&quot; \/&gt;\n    &lt;\/bean&gt;\n<\/pre>\n<h2>Checking the FTP directory<\/h2>\n<p>If you want Spring Integration to check the FTP server on a regular base for new files, you can wire up an inbound channel adapter with a cron expression like this:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n    &lt;si:inbound-channel-adapter id=&quot;zipInboundChannelAdapter&quot; ref=&quot;ftpInboundAdapter&quot; method=&quot;getFilesFromFTPClient&quot; channel=&quot;zipFilesChannel&quot;&gt;\n        &lt;si:poller max-messages-per-poll=&quot;1&quot;&gt;\n            &lt;si:cron-trigger expression=&quot;0 0\/5 * ? * *&quot; \/&gt;\n        &lt;\/si:poller&gt;\n    &lt;\/si:inbound-channel-adapter&gt;\n<\/pre>\n<p>The channel attribute of the inbound channel adapter specifies the output channel. So in our case this will be the message with the list of zip files that is put onto this channel. This channel can then be used to send the message to wherever you want.<\/p>\n<p>As you could see, it was relatively easy to connect with a FTP server and retrieve the files. I hope this post helped you in setting up your own FTP connection with Spring Integration. All that rests us is waiting for official release of the Spring Integration Adapters.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For one of our project I needed to read zip files from a FTP server and import the content in a system. In this post I will explain how I have used the Spring Integration to connect with a FTP server and retrieve Zip files.<\/p>\n","protected":false},"author":102,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"content-type":"","footnotes":""},"categories":[10,94],"tags":[205,11,70,190],"class_list":["post-2389","post","type-post","status-publish","format-standard","hentry","category-development","category-spring","tag-ftp","tag-java","tag-spring","tag-spring-integration"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Connecting to FTP server with Spring Integration - Trifork Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/trifork.nl\/blog\/connecting-to-ftp-server-with-spring-integration\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Connecting to FTP server with Spring Integration - Trifork Blog\" \/>\n<meta property=\"og:description\" content=\"For one of our project I needed to read zip files from a FTP server and import the content in a system. In this post I will explain how I have used the Spring Integration to connect with a FTP server and retrieve Zip files.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/trifork.nl\/blog\/connecting-to-ftp-server-with-spring-integration\/\" \/>\n<meta property=\"og:site_name\" content=\"Trifork Blog\" \/>\n<meta property=\"article:published_time\" content=\"2010-07-21T13:07:50+00:00\" \/>\n<meta name=\"author\" content=\"Roberto van der Linden\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Roberto van der Linden\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/trifork.nl\/blog\/connecting-to-ftp-server-with-spring-integration\/\",\"url\":\"https:\/\/trifork.nl\/blog\/connecting-to-ftp-server-with-spring-integration\/\",\"name\":\"Connecting to FTP server with Spring Integration - Trifork Blog\",\"isPartOf\":{\"@id\":\"https:\/\/trifork.nl\/blog\/#website\"},\"datePublished\":\"2010-07-21T13:07:50+00:00\",\"author\":{\"@id\":\"https:\/\/trifork.nl\/blog\/#\/schema\/person\/037974cf3e24a7b09a93770b190d6e35\"},\"breadcrumb\":{\"@id\":\"https:\/\/trifork.nl\/blog\/connecting-to-ftp-server-with-spring-integration\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/trifork.nl\/blog\/connecting-to-ftp-server-with-spring-integration\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/trifork.nl\/blog\/connecting-to-ftp-server-with-spring-integration\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/trifork.nl\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Connecting to FTP server with Spring Integration\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/trifork.nl\/blog\/#website\",\"url\":\"https:\/\/trifork.nl\/blog\/\",\"name\":\"Trifork Blog\",\"description\":\"Keep updated on the technical solutions Trifork is working on!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/trifork.nl\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/trifork.nl\/blog\/#\/schema\/person\/037974cf3e24a7b09a93770b190d6e35\",\"name\":\"Roberto van der Linden\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/trifork.nl\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/afe49faf7ef8dd3753baefb334568b10?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/afe49faf7ef8dd3753baefb334568b10?s=96&d=mm&r=g\",\"caption\":\"Roberto van der Linden\"},\"url\":\"https:\/\/trifork.nl\/blog\/author\/roberto\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Connecting to FTP server with Spring Integration - Trifork Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/trifork.nl\/blog\/connecting-to-ftp-server-with-spring-integration\/","og_locale":"en_US","og_type":"article","og_title":"Connecting to FTP server with Spring Integration - Trifork Blog","og_description":"For one of our project I needed to read zip files from a FTP server and import the content in a system. In this post I will explain how I have used the Spring Integration to connect with a FTP server and retrieve Zip files.","og_url":"https:\/\/trifork.nl\/blog\/connecting-to-ftp-server-with-spring-integration\/","og_site_name":"Trifork Blog","article_published_time":"2010-07-21T13:07:50+00:00","author":"Roberto van der Linden","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Roberto van der Linden","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/trifork.nl\/blog\/connecting-to-ftp-server-with-spring-integration\/","url":"https:\/\/trifork.nl\/blog\/connecting-to-ftp-server-with-spring-integration\/","name":"Connecting to FTP server with Spring Integration - Trifork Blog","isPartOf":{"@id":"https:\/\/trifork.nl\/blog\/#website"},"datePublished":"2010-07-21T13:07:50+00:00","author":{"@id":"https:\/\/trifork.nl\/blog\/#\/schema\/person\/037974cf3e24a7b09a93770b190d6e35"},"breadcrumb":{"@id":"https:\/\/trifork.nl\/blog\/connecting-to-ftp-server-with-spring-integration\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/trifork.nl\/blog\/connecting-to-ftp-server-with-spring-integration\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/trifork.nl\/blog\/connecting-to-ftp-server-with-spring-integration\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/trifork.nl\/blog\/"},{"@type":"ListItem","position":2,"name":"Connecting to FTP server with Spring Integration"}]},{"@type":"WebSite","@id":"https:\/\/trifork.nl\/blog\/#website","url":"https:\/\/trifork.nl\/blog\/","name":"Trifork Blog","description":"Keep updated on the technical solutions Trifork is working on!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/trifork.nl\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/trifork.nl\/blog\/#\/schema\/person\/037974cf3e24a7b09a93770b190d6e35","name":"Roberto van der Linden","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/trifork.nl\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/afe49faf7ef8dd3753baefb334568b10?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/afe49faf7ef8dd3753baefb334568b10?s=96&d=mm&r=g","caption":"Roberto van der Linden"},"url":"https:\/\/trifork.nl\/blog\/author\/roberto\/"}]}},"_links":{"self":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/posts\/2389","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/users\/102"}],"replies":[{"embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/comments?post=2389"}],"version-history":[{"count":0,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/posts\/2389\/revisions"}],"wp:attachment":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/media?parent=2389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/categories?post=2389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/tags?post=2389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}