Apache Camel之FTP组件:简单地从Ftp服务器下载文件到本地文件夹

前期准备:在自己的电脑上搭建FTP服务器

  • 非Spring实现

    maven依赖:
    在一个项目里有关于camel的版本要保持一致!!!

<dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-core</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-jms</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-ftp</artifactId>
      <version>2.15.6</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
    </dependency>

具体实现:

public class RouteBuilderExample {

    public static void main(String args[]) throws Exception {
        CamelContext context = new DefaultCamelContext();

        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() {
                 from("ftp://ftpTest@172.16.1.13:21/FtpDownLoad?password=123456").
                 to("file:E:/FtpTestFile");
            }
        });
        context.start();
        Thread.sleep(10000);
        context.stop();
    }
}
  • Spring实现
    这里写图片描述

    camel-context.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://camel.apache.org/schema/spring 
       http://camel.apache.org/schema/spring/camel-spring.xsd
">

  <bean id="ftpToFile" class="camelinaction.FtpToFile"/>

  <!--装载 CamelContext -->

  <camelContext xmlns="http://camel.apache.org/schema/spring">  
    <!-- Java DSL 方式 -->
    <routeBuilder ref="ftpToFile"/>

    <!-- Spring DSL 方式 -->
   <route>
     <from uri="ftp://ftpTest@172.16.1.13:21/FtpDownLoad?password=123456"/>
     <to uri="file:E:/FtpTestFile"/>
   </route>
  </camelContext>
</beans>

FtpToFile类:

public class FtpToFile extends RouteBuilder{
    @Override
    public void configure() throws Exception { 
      from("ftp://ftpTest@172.16.1.13:21/FtpDownLoad?password=123456").
      to("file:E:/360");
    }
}

最后在win+R窗口进入项目所在目录运行maven我的是:
这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值