我在通过SOAP调用Web服务时会得到以下例外:
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
原因:
下面是我的代码段。我正在使用Datatype -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);
根据例外,它期望字符串为附件内容。
尝试与此类似。
AttachmentPart ap1 = m.createAttachmentPart();
ap1.setContent(contentString, "text/plain");
m.addAttachmentPart(ap1);