Mule 3.4.0中对Ftp协议的上传下载的应用
<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:ftp="http://www.mulesoft.org/schema/mule/ftp" xmlns:file="http://www.mulesoft.org/schema/mule/file" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/ftp http://www.mulesoft.org/schema/mule/ftp/current/mule-ftp.xsd"><!--连接 FTP 服务器的连接器从 ftp 服务器上下载文件的方法--> <ftp:connector name = "ftpConnector" binary = "true" validateConnections = "true" /> <model name = "ftp-model" > <service name = "ftp-reader" > <!-- 连接 FTP 服务器的入口节点,在这里配置 ftp 的 ip ,账户和密码 利用 file:filename-wildcard -filter 确认只下载 txt 文件 --> <inbound > <ftp:inbound-endpoint user = "root" binary = "true" path = "/home/test" password = "123456" host = "10.100.102.8" port = "21" pollingFrequency = "10000" connector-ref = "ftpConnector" > <file:filename-wildcard-filter pattern = "*.txt" /> </ftp:inbound-endpoint > </inbound > <!-- 连接 FTP 服务器的出口节点,在这里配置地址,将文件下载到 e:/download 目录下 同时利用 outputPattern ,定义下载的文件和源文件同名同类型 --> <outbound > <pass-through-router > <file:outbound-endpoint path = "/e:/download" outputPattern = "[header:originalFilename]" /> </pass-through-router > </outbound > </service > </model ></mule >
import org.mule.api.MuleContext;import org.mule.api.MuleException;import org.mule.api.context.MuleContextFactory;import org.mule.config.spring.SpringXmlConfigurationBuilder;import org.mule.context.DefaultMuleContextFactory;/** * <p>功能描述,该部分必须以中文句号结尾。<p> * * 创建日期 2013-8-16<br> * @author $Author$<br> * @version $Revision$ $Date$ * @since 3.0.0 */public class MuleFtpDownloadMain { public static void main(String[] args) { try { String configFile = "mule-download-config.xml"; String[] configFileArr = new String[] {configFile }; MuleContextFactory muleContextFactory = new DefaultMuleContextFactory(); MuleContext context = muleContextFactory .createMuleContext(new SpringXmlConfigurationBuilder(configFileArr)); context.start(); } catch (MuleException t) { t.printStackTrace(); } }}
<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:ftp="http://www.mulesoft.org/schema/mule/ftp" xmlns:file="http://www.mulesoft.org/schema/mule/file" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/ftp http://www.mulesoft.org/schema/mule/ftp/current/mule-ftp.xsd"><!-- 上传文件到 ftp 服务器上的办法--> <file:connector name = "fileConnector" pollingFrequency = "6000" /><!--连接 ftp 服务器的连接器--> <ftp:connector name = "ftpConnector" /> <model name = "model" > <service name = "service" > <inbound > <!-- 利用 fileConnector 读取 e:/tmp 下的文件 --> <file:inbound-endpoint path = "/e:/upload" connector-ref = "fileConnector" > </file:inbound-endpoint > </inbound > <outbound > <!-- 利用 ftpConnector 将文件传输到 ftp 服务器上,这里配置了服务器的 ip 和账户密码 --> <pass-through-router > <ftp:outbound-endpoint host = "10.100.102.8" port = "21" user = "root" password = "123456" path = "/home/test" connector-ref = "ftpConnector" /> </pass-through-router > </outbound > </service > </model ></mule >
?
?
测试
import org.mule.api.MuleContext;import org.mule.api.MuleException;import org.mule.api.context.MuleContextFactory;import org.mule.config.spring.SpringXmlConfigurationBuilder;import org.mule.context.DefaultMuleContextFactory;/** * 检查是否发布成功的请求路径为: * http://localhost:9090/hello?wsdl * <p>功能描述,该部分必须以中文句号结尾。<p> * * 创建日期 2013-8-16<br> * @author $Author$<br> * @version $Revision$ $Date$ * @since 3.0.0 */public class MuleFtpUploadMain { public static void main(String[] args) { try { String configFile = "mule-upload-config.xml"; String[] configFileArr = new String[] {configFile }; MuleContextFactory muleContextFactory = new DefaultMuleContextFactory(); MuleContext context = muleContextFactory .createMuleContext(new SpringXmlConfigurationBuilder(configFileArr)); context.start(); } catch (MuleException t) { t.printStackTrace(); } }}
?