Copy to ClipboardCopied!Toggle word wrapToggle overflow
我们使用可以从 java 代码处理邮件的处理器,而不是记录以下邮件:
public void process(Exchange exchange) throws Exception {
// the API is a bit clunky so we need to loop
AttachmentMessage attachmentMessage = exchange.getMessage(AttachmentMessage.class);
Map<String, DataHandler> attachments = attachmentMessage.getAttachments();
if (attachments.size() > 0) {
for (String name : attachments.keySet()) {
DataHandler dh = attachments.get(name);
// get the file name
String filename = dh.getName();
// get the content and convert it to byte[]
byte[] data = exchange.getContext().getTypeConverter()
.convertTo(byte[].class, dh.getInputStream());
// write the data to a file
FileOutputStream out = new FileOutputStream(filename);
out.write(data);
out.flush();
out.close();
}
}
}
public void process(Exchange exchange) throws Exception {
// the API is a bit clunky so we need to loop
AttachmentMessage attachmentMessage = exchange.getMessage(AttachmentMessage.class);
Map<String, DataHandler> attachments = attachmentMessage.getAttachments();
if (attachments.size() > 0) {
for (String name : attachments.keySet()) {
DataHandler dh = attachments.get(name);
// get the file name
String filename = dh.getName();
// get the content and convert it to byte[]
byte[] data = exchange.getContext().getTypeConverter()
.convertTo(byte[].class, dh.getInputStream());
// write the data to a file
FileOutputStream out = new FileOutputStream(filename);
out.write(data);
out.flush();
out.close();
}
}
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow
如您所见到处理附件的 API 是一个位线,但这样您就可以得到 javax.activation.DataHandler,因此您可以使用标准 API 处理附件。