在saaj呼吁通过肥皂发送附件期间的例外

时间:2017-03-02 13:21:12

标签: java web-services soap soap-client webservice-client

通过soap调用webservice调用时,我收到以下异常:

Mar 02, 2017 6:24:39 PM 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:1337)
    at com.abc.client.someClient.method(someClient.java:66)
    at virusscan.main.test.main(test.java:46)
Caused by: java.io.IOException: "text/plain" DataContentHandler requires String object, was given object of type class java.io.FileInputStream
    at com.sun.xml.internal.messaging.saaj.soap.StringDataContentHandler.writeTo(StringDataContentHandler.java:139)
    at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:896)
    at javax.activation.DataHandler.writeTo(DataHandler.java:329)
    at com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeBodyPart.writeTo(MimeBodyPart.java:973)
    at com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeMultipart.writeTo(MimeMultipart.java:284)
    at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.writeTo(MessageImpl.java:1328)
    ... 2 more

原因:

以下是我的代码段。我发送的数据类型为-text / plain的文本文件。任何人都可以帮助我理解这一点。什么是错的。我正在使用java 1.7

        SOAPConnection connection =factory.createConnection();  
        msgFactory = MessageFactory.newInstance();
        SOAPMessage reqMessage = msgFactory.createMessage();
        AttachmentPart attachment =  reqMessage.createAttachmentPart();
        attachment.setContent(fileStream,dataType);
        reqMessage.addAttachmentPart(attachment);

        StringBuffer sbuf=new StringBuffer();
        sbuf.append("\n").append(reqMessage.toString()); 
        System.out.println(sbuf);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        reqMessage.writeTo(baos);                     
        sbuf.append(baos.toString());         

        endpoint = new URL(endPointAddress);

        responseMsg= (SOAPMessage)connection.call(reqMessage, endpoint);

1 个答案:

答案 0 :(得分:0)

根据例外,它期望String作为附件内容。

尝试类似于此。

 AttachmentPart ap1 = m.createAttachmentPart();
 ap1.setContent(contentString, "text/plain");
 m.addAttachmentPart(ap1);