Mule 3.6.1实现ftp上传文件

本文介绍了一个Java类,用于将本地文件转换为二进制格式,并通过Mule ESB配置将其上传到FTP服务器的过程。此外,还展示了如何使用自定义转换器将文件内容作为消息发送至JMS队列。
1. 编写java类,该类的主要的目的是将文件转化为二进制
package com.sigmatrix.ftp.service;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractMessageTransformer;

public class CreateFile extends AbstractMessageTransformer{

     @Override
     public Object transformMessage(MuleMessage message, String outputEncoding)
               throws TransformerException {
          File file = new File("F:\\001.jpg");
          return getBytesFromFile(file);
     }
    
     public static byte[] getBytesFromFile(File f){ 
        if (f == null){ 
            return null; 
        } 
        try{ 
            FileInputStream stream = new FileInputStream(f); 
            ByteArrayOutputStream out = new ByteArrayOutputStream(1000); 
            byte[] b = new byte[1000]; 
            int n; 
            while ((n = stream.read(b)) != -1) 
                out.write(b, 0, n); 
                stream.close(); 
                out.close(); 
            return out.toByteArray(); 
        } catch (IOException e){ 
            e.printStackTrace(); 
        } 
        return null; 
    } 
}
2. 配置.xml文件
<?xmlversion="1.0"encoding="UTF-8"?>

<mulexmlns:sftp="http://www.mulesoft.org/schema/mule/sftp"
     xmlns:ftp="http://www.mulesoft.org/schema/mule/ftp"xmlns:file="http://www.mulesoft.org/schema/mule/file"
     xmlns:jms="http://www.mulesoft.org/schema/mule/jms"xmlns:http="http://www.mulesoft.org/schema/mule/http"
     xmlns:json="http://www.mulesoft.org/schema/mule/json"xmlns="http://www.mulesoft.org/schema/mule/core"
     xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
     xmlns:spring="http://www.springframework.org/schema/beans"version="CE-3.6.1"
     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-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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd
http://www.mulesoft.org/schema/mule/sftp http://www.mulesoft.org/schema/mule/sftp/current/mule-sftp.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/ftp http://www.mulesoft.org/schema/mule/ftp/current/mule-ftp.xsd">
     <jms:activemq-connectorname="Active_MQ"
           username="admin"password="admin"brokerURL="tcp://192.168.1.146:61616"
           validateConnections="true"doc:name="Active MQ"/>
   <http:listener-configname="HTTP_Listener_Configuration"host="localhost"port="65000"doc:name="HTTP Listener Configuration"/>
     <flowname="emailWebService-flow">
       <http:listenerconfig-ref="HTTP_Listener_Configuration"path="/ftp"doc:name="HTTP"/>
       <custom-transformerclass="com.sigmatrix.ftp.service.CreateFile"doc:name="File To binary"/>
       <set-propertypropertyName="imageUrl"value="#[server.dateTime+&quot;.jpg&quot;]"doc:name="Set ImageName Property"/>
           <ftp:outbound-endpoint host="124.205.21.162"
                port="21"path="/home/wwwroot/ftpuser/upload/artVerification/art/mule/"user="ftp_mssc_user"
                password="123456"responseTimeout="10000"doc:name="FTP"passive="false"binary="true"outputPattern="#[message.outboundProperties.imageUrl]"/>
       <expression-componentdoc:name="Set Message To MQ"><![CDATA[payload = "{\"imgUrl\":\"http://192.168.1.211:86/upload/artVerification/art/mule/"+message.outboundProperties.imageUrl+"\"}";]]></expression-component>
       <flow-refname="send-jms-after-upload-to-ftpSub_Flow"doc:name="send-jms-after-upload-to-ftpSub_Flow"/>
           <echo-componentdoc:name="Echo"/>
     </flow>
   <sub-flowname="send-jms-after-upload-to-ftpSub_Flow">
       <asyncdoc:name="Async">
           <jms:outbound-endpointqueue="imageIndexQueue"connector-ref="Active_MQ"doc:name="JMS"/>
       </async>
   </sub-flow>
</mule>

流程图如下
3. 这样执行就可以将选中文件上传到FTP服务器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值