Java – soapmessage writeto without attachments
•
Java
I use soapmessage Writeto (OutputStream) to record web service messages The problem is that it also writes attachments It takes up space and binary attachments are not readable Is there any way to record emails without attachments, such as wrapping paper?
There must be a better solution than this
ByteArrayOutputStream out = new ByteArrayOutputStream(); message.writeTo(out); StringBuilder builder = new StringBuilder(out.toString()); int indexOfAttachment = builder.indexOf("------="); if (indexOfAttachment != -1) { return builder.substring(0,indexOfAttachment); } return builder.toString();
Sample message
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> <S:Header /> <S:Body> <ns2:wsGetObjectByIDResponse xmlns:ns2="http://xxx.com/" xmlns:ns3="http://yyy.com/"> <return> <serviceResponse status="OK" /> <contentData formatName="jpeg_lres" objectContent="cid:e677f02c-002a-4c2c-8fd9-a3acdba5ad11@example.jaxws.sun.com" objectName="Smlouva1.jpg" /> </return> </ns2:wsGetObjectByIDResponse> </S:Body> </S:Envelope> ------=_Part_9_-806948376.1352979403086 Content-Type: image/jpeg Content-ID: <e677f02c-002a-4c2c-8fd9-a3acdba5ad11@example.jaxws.sun.com> Content-@R_959_301@: binary ����\x00JFIF\x00\x00�\x00�\x00\x00��\x00C\x00
Solution
In fact, there is a way to make it cleaner
This is my code:
// Get the Envelope Source Source src = message.getSOAPPart().getContent() ; // Transform the Source into a StreamResult to get the XML Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT,"no"); StreamResult result = new StreamResult(new StringWriter()); transformer.transform(src,result); String xmlString = result.getWriter().toString();
You can then record the xmlstring, which corresponds only to the envelope section
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码