2013-03-01 69 views
1

我試圖將郵件附件從IMAP入站端點傳遞給JAVA組件,但失敗。錯誤消息顯示「消息有效負載的類型爲:MimeBodyPart $ MimePartDataHandler」。如何使用MULE ESB將郵件附件傳遞給POJO對象

附件中的文件是Excel文件,我可以連接Indound File端點,但我想用IMAP端點代替。

如何獲取傳遞給typw文件的JAVA組件的消息?

這裏是我的騾子配置:

<flow name="imaptestflow" doc:name="imaptestflow"> 
    <imap:inbound-endpoint user="XXXXXXXX" 
     password="XXXXX" host="XXXXXXX" port="143" doc:name="IMAP" disableTransportTransformer="true"/> 
    <expression-transformer evaluator="attachments-list" expression="*.xls" doc:name="Expression"/> 

    <collection-splitter doc:name="Collection Splitter"/> 
    <all doc:name="All"> 
     <processor-chain> 
      <component class="xlsFileRead" doc:name="Java"/> 
     </processor-chain> 
     <processor-chain> 
      <file:outbound-endpoint path="c:\out" outputPattern="#[groovy:payload.getName()]" doc:name="File"/> 
     </processor-chain> 
    </all> 
</flow> 

的Java的類是:

import java.io.File; 
import java.io.IOException; 
import java.util.List; 
import jxl.Cell; 
import jxl.CellType; 
import jxl.Sheet; 
import jxl.Workbook; 
import jxl.read.biff.BiffException; 
public class xlsFileRead { 
public void readFromFile(Object input)throws IOException { 
     Workbook w; 
     try { 
      w = Workbook.getWorkbook((File)input); 
      // Get the first sheet 
      Sheet sheet = w.getSheet(0); 
      // Loop over first 10 column and lines 
      for (int i = 5; i < sheet.getRows(); i++) { 
      for (int j = 0; j < sheet.getColumns(); j++) { 
       Cell cell = sheet.getCell(j, i); 
       CellType type = cell.getType(); 
       if (type == CellType.LABEL) { 
       System.out.print(cell.getContents() + ";"); 
       } 
       if (type == CellType.NUMBER) { 
       System.out.print(cell.getContents() + ";"); 
       } 
      } 
      System.out.println(i); 
      } 
     } catch (BiffException e) { 
      e.printStackTrace(); 
     } 
} 
} 

而且從騾控制檯的例外是:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
+ Started app 'imaptest'         + 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
INFO 2013-03-03 19:35:17,592 [[imaptest].imaptestflow.stage1.02] org.mule.api.processor.LoggerMessageProcessor: [rtdata2.xls, rtdata.xlsx] 
INFO 2013-03-03 19:35:17,598 [[imaptest].imaptestflow.stage1.02] org.mule.lifecycle.AbstractLifecycleManager: Initialising: 'connector.file.mule.default.dispatcher.1635258243'. Object is: FileMessageDispatcher 
INFO 2013-03-03 19:35:17,598 [[imaptest].imaptestflow.stage1.02] org.mule.lifecycle.AbstractLifecycleManager: Starting: 'connector.file.mule.default.dispatcher.1635258243'. Object is: FileMessageDispatcher 
INFO 2013-03-03 19:35:17,601 [[imaptest].imaptestflow.stage1.02] org.mule.transport.file.FileConnector: Writing file to: C:\OUT\18a0c736-8431-11e2-b133-e9e7c6fca1c6rtdata2.xls 
ERROR 2013-03-03 19:35:17,674 [[imaptest].imaptestflow.stage1.02] org.mule.exception.DefaultMessagingExceptionStrategy: 
******************************************************************************** 
Message    : Component that caused exception is: DefaultJavaComponent{imaptestflow.commponent.569080239}. Message payload is of type: MimeBodyPart$MimePartDataHandler 
Code     : MULE_ERROR--2 
-------------------------------------------------------------------------------- 
Exception stack is: 
    1. javax.mail.internet.MimeBodyPart$MimePartDataHandler cannot be cast to java.io.File (java.lang.ClassCastException)xlsFileRead:18 (null) 
    2. Component that caused exception is: DefaultJavaComponent{imaptestflow.commponent.569080239}. Message payload is of type: MimeBodyPart$MimePartDataHandler (org.mule.component.ComponentException) org.mule.component.DefaultComponentLifecycleAdapter:352 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/component/ComponentException.html) 
-------------------------------------------------------------------------------- 
Root Exception stack trace: 
java.lang.ClassCastException: javax.mail.internet.MimeBodyPart$MimePartDataHandler cannot be cast to java.io.File 
at xlsFileRead.readFromFile(xlsFileRead.java:18) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
+ 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything) 
******************************************************************************** 
+0

我不知道什麼騾子,但在一般的郵件附件,應通過流(在數據庫中的BLOB一樣)訪問。你應該從消息獲得附件,然後從附件獲取內容。 – Nemanja 2013-03-01 23:29:47

+1

顯示完整的例外情況,不可能從您分享的內容中判斷出什麼問題。 – 2013-03-03 02:20:17

+0

我現在已經添加了從流中調用的Java類以及完整的異常。任何投入都歡迎!先謝謝你! – user1912657 2013-03-03 19:02:22

回答

1

我現在已經完成了一項工作ound。我將附件寫入一個文件,然後再讀一遍。 它的工作原理,現在已經夠用了。

下面的代碼:

public class xlsFileRead { 

public void readFromFile(Object input)throws IOException { 

    DataHandler handler = (DataHandler)input; 
    String tempfilenm = handler.getName(); 
    InputStream is = handler.getInputStream(); 
    File tempfile =new File("c:\\tempdev\\" + tempfilenm); 
    OutputStream os = new FileOutputStream(tempfile); 
    byte[] buffer = new byte[1024]; 
    int bytesRead = 0; 
    while ((bytesRead = is.read(buffer)) != -1) { 
    os.write(buffer,0,bytesRead); 
     }  
Workbook w; 
    try { 
     w = Workbook.getWorkbook(tempfile); 
     // Get the first sheet 
     Sheet sheet = w.getSheet(0); 
     // Loop over first 10 column and lines 
     for (int i = 5; i < sheet.getRows(); i++) { 
     for (int j = 0; j < sheet.getColumns(); j++) { 

      Cell cell = sheet.getCell(j, i); 
      CellType type = cell.getType(); 
      if (type == CellType.LABEL) { 
      System.out.print(cell.getContents() + ";"); 
      } 

      if (type == CellType.NUMBER) { 
      System.out.print(cell.getContents() + ";"); 
      } 

     } 
     System.out.println(i); 
     } 
    } catch (BiffException e) { 
     e.printStackTrace(); 
    } 

}}

+0

上述解決方案中的DataHandler類的來源是什麼庫/包? – GarySharpe 2013-09-27 17:36:08

相關問題