这些单元测试向您展示如何使用CXF从MTOM消息中提取附件.如果将来不存在此链接,我将内联其中一个测试:
private MessageImpl msg;
@Before
public void setUp() throws Exception {
msg = new MessageImpl();
Exchange exchange = new ExchangeImpl();
msg.setExchange(exchange);
}
@Test
public void testDeserializerMtom() throws Exception {
InputStream is = getClass().getResourceAsStream("mimedata");
String ct = "multipart/related; type=\"application/xop+xml\"; "
+ "start=\"<soap.xml@xfire.codehaus.org>\"; "
+ "start-info=\"text/xml; charset=utf-8\"; "
+ "boundary=\"----=_Part_4_701508.1145579811786\"";
msg.put(Message.CONTENT_TYPE, ct);
msg.setContent(InputStream.class, is);
AttachmentDeserializer deserializer = new AttachmentDeserializer(msg);
deserializer.initializeAttachments();
InputStream attBody = msg.getContent(InputStream.class);
assertTrue(attBody != is);
assertTrue(attBody instanceof DelegatingInputStream);
Collection<Attachment> atts = msg.getAttachments();
assertNotNull(atts);
Iterator<Attachment> itr = atts.iterator();
assertTrue(itr.hasNext());
Attachment a = itr.next();
assertNotNull(a);
InputStream attIs = a.getDataHandler().getInputStream();
// check the cached output stream
ByteArrayOutputStream out = new ByteArrayOutputStream();
IOUtils.copy(attBody, out);
assertTrue(out.toString().startsWith("<env:Envelope"));
// try streaming a character off the wire
assertTrue(attIs.read() == '/');
assertTrue(attIs.read() == '9');
}
Run Code Online (Sandbox Code Playgroud)
在您的情况下,ct将来自响应的内容类型标头.这"mimedata"将是回复的内容.
无需使用CXF,标准的javax.mail.internet.MimeMultipart类可以完成这项工作,并且它非常易于使用(也可以创建MTOM请求).
这是一个非常简单的示例,用于解码部分MTOM响应:
MimeMultipart mp = new MimeMultipart(new ByteArrayDataSource(data, contentType));
int count = mp.getCount();
for (int i = 0; i < count; i++) {
BodyPart bp = mp.getBodyPart(i);
bp.saveFile(filepath + "_" + i);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5406 次 |
| 最近记录: |