1

我正在从 SOAP 消息中获取附件并在我的应用程序中使用它们。
我不会从原始邮件中删除附件。之后,我尝试使用以下代码将包含附件(SwA)的原始 SOAPMessage转换为字符串:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
soapMsg.writeTo(stream);
String message = new String(stream.toByteArray(), "utf-8");

它抛出以下异常:

com.sun.xml.internal.messaging.saaj.soap.MessageImpl writeTo
SEVERE: SAAJ0540: Error during saving a multipart message
com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Error during saving a multipart message
    at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.writeTo(MessageImpl.java:1233)
    at main.SOAPMTOMUtilityTest.main(SOAPMTOMUtilityTest.java:161)
Caused by: com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException: no content
    at com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeBodyPart.writeTo(MimeBodyPart.java:905)
    at com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeMultipart.writeTo(MimeMultipart.java:266)
    at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.writeTo(MessageImpl.java:1224)
    ... 1 more

CAUSE:

com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException: no content
    at com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeBodyPart.writeTo(MimeBodyPart.java:905)
    at com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeMultipart.writeTo(MimeMultipart.java:266)
    at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.writeTo(MessageImpl.java:1224)
    at main.SOAPMTOMUtilityTest.main(SOAPMTOMUtilityTest.java:161)

有人知道吗?

_谢谢,布山

4

1 回答 1

0

也许尝试使用 StringBuilder 而不是 String?如下所示使用StringBuilder.append()对我有用,而我根本无法让新实例化的(如您使用的)String()工作。

        // This stringbuilder will hold the initial message we sent to SOA
        StringBuilder soapMsg = new StringBuilder();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        SOAPMessage message = getSoapMessageFromString(foo);

        // Write the initial message to the byte array output string
        message.writeTo(baos);

        // append the baos data (initial message) to the stringbuilder for
        // processing
        soapMsg.append(baos.toString());

        // Reset the baos for use on the response message
        baos.reset();
于 2015-03-03T16:22:03.157 回答