1

我正在尝试将邮件附件从 IMAP 入站端点传递到 JAVA 组件,但它失败了。错误消息显示“消息有效负载的类型为:MimeBodyPart$MimePartDataHandler”。

附件中的文件是 Excel 文件,我可以连接 Indound File 端点,但我想改用 IMAP 端点。

如何获取传递给 typw FILE 的 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();
        }
}
}

Mule 控制台的异常是:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 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)
********************************************************************************
4

1 回答 1

1

我现在已经解决了。我将附件写入文件,然后再次读取。它有效,现在已经足够了。

这是代码:

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();
    }

} }

于 2013-03-05T21:30:36.200 回答