I am using SAAJ 1.3 for sending attachments from client to server.
My client code looks like this...
My server-side interceptor looks like this...Code:SOAPMessage message = messageFactory.createMessage(); SOAPEnvelope envelope = message.getSOAPPart().getEnvelope(); SOAPBody soapBody = envelope.getBody(); SOAPHeader header = envelope.getHeader(); SOAPFactory soapFactory = SOAPFactory.newInstance(); DataSource dataSource = new FileDataSource("C:/test.txt"); DataHandler dataHandler= new DataHandler(dataSource); ByteArrayOutputStream attachmentStream = new ByteArrayOutputStream(); InputStream is = new BufferedInputStream(dataHandler.getInputStream()); int size = is.available(); int bytesRead = 0; byte[] buffer = new byte[size]; while ((bytesRead = is.read(buffer, 0, size)) != -1) { attachmentStream.write(buffer, 0, bytesRead); } dataHandler.writeTo(attachmentStream); is.close(); attachmentStream.close(); AttachmentPart attachmentPart = message.createAttachmentPart(dataHandler); attachmentPart.setContentId("A1"); message.addAttachmentPart(attachmentPart); message.saveChanges(); message.writeTo(System.out); // and im sending the soap message to the server....
Except for a .txt file , it prints out some erroneous special characters whenever Doc/jpeg file is attached.Code:public class HeaderLogger implements EndpointInterceptor { public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception { WebServiceMessage requestMessage = messageContext.getRequest(); AbstractSoapMessage absSoapMessage = (AbstractSoapMessage) requestMessage; SaajSoapMessage tmpSaaj =(SaajSoapMessage) absSoapMessage; SOAPMessage tmpSoapmsg = tmpSaaj.getSaajMessage(); java.util.Iterator iterator = tmpSoapmsg.getAttachments(); while (iterator.hasNext()) { AttachmentPart attachment = (AttachmentPart)iterator.next(); Object content = attachment.getContent(); System.out.println(attachment.getContent().toString());
Is it like we have to encode it to some other format before sending the attachment to avoid those special characters.
I'm using springWS 1.0 rc-2 on Apache Tomcat 5.0 and the jars used for SAAJ are saaj-api-1.3.jar and saaj-impl-1.3.jar.


Reply With Quote