我正在尝试将邮件附件从IMAP入站端点传递到JAVA组件,但它失败了。错误消息显示“消息有效内容的类型为:MimeBodyPart $ MimePartDataHandler”。
附件中的文件是Excel文件,我可以连接Indound File端点,但我想使用IMAP端点。
如何将消息传递给typw文件的JAVA组件?
这是我的Mule配置:
<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-Class是:
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)
********************************************************************************
答案 0 :(得分: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();
}
} }