Mule 3.6.1实现ftp上传文件

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文件
xml version = "1.0" encoding = "UTF-8" ?>

< mule xmlns: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-connector name = "Active_MQ"
            username = "admin" password = "admin" brokerURL = "tcp://192.168.1.146:61616"
            validateConnections = "true" doc:name = "Active MQ" />
    < http:listener-config name = "HTTP_Listener_Configuration" host = "localhost" port = "65000" doc:name = "HTTP Listener Configuration" />
      < flow name = "emailWebService-flow" >
        < http:listener config-ref = "HTTP_Listener_Configuration" path = "/ftp" doc:name = "HTTP" />
        < custom-transformer class = "com.sigmatrix.ftp.service.CreateFile" doc:name = "File To binary" />
        < set-property propertyName = "imageUrl" value = "#[server.dateTime+".jpg"]" 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-component doc:name = "Set Message To MQ" > payload = "{\"imgUrl\":\"http://192.168.1.211:86/upload/artVerification/art/mule/"+message.outboundProperties.imageUrl+"\"}"; ]]> expression-component >
        < flow-ref name = "send-jms-after-upload-to-ftpSub_Flow" doc:name = "send-jms-after-upload-to-ftpSub_Flow" />
            < echo-component doc:name = "Echo" />
      flow >
    < sub-flow name = "send-jms-after-upload-to-ftpSub_Flow" >
        < async doc:name = "Async" >
            < jms:outbound-endpoint queue = "imageIndexQueue" connector-ref = "Active_MQ" doc:name = "JMS" />
        async >
    sub-flow >
mule >

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

你可能感兴趣的:(Java)