SoapMessage.writeTo为带有附件的SOAPMessage抛出无内容异常

时间:2015-03-02 09:10:35

标签: java soap saaj

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

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)

有没有人对此有所了解?

_Thanks, 普山

1 个答案:

答案 0 :(得分: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();