2013-03-01 70 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

相关问题