I am trying to pass different files using a SOAP with attachments (JAXM). It works fine for an xml file as the attchment (for example) but any other type of file is causing the following error: "java.net.SocketException: Unexpected end of file from server" The code I am using to pass create the attachment is: File outputFile = new File("C:/myfile.bmp"); //Build the call:- //Get a connection SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance(); SOAPConnection connSOAP = scf.createConnection(); //Create a message MessageFactory mf = MessageFactory.newInstance(); SOAPMessagemsg = mf.createMessage(); //Access the elements of the message SOAPPart part = msg.getSOAPPart(); SOAPEnvelope envelope = part.getEnvelope(); SOAPHeader header = envelope.getHeader(); SOAPBody body = envelope.getBody(); //Add a new SOAP element Name name = envelope.createName("loadFile"); SOAPElement child = body.addBodyElement(name); //Add content name = envelope.createName("filename"); SOAPElement filename = child.addChildElement(name); filename.addTextNode(outputFile.getAbsolutePath()); //Add attachment URL fileurl = outputFile.toURL(); DataHandler dh = new DataHandler(fileurl);
Hi Kyle not exactly certain of what you mean, but below is the actual dump stack I get. If it isn't encoding it like you say, how do I go about encoding it? Alternatively how about I write the file to a byte array and attach that? Would that work.. although how to write a whole file to an an array I'm not sure... Any advice appreciated... Thanks Kyle
Then the client isn't even getting through the headers before something odd happens. I ran into the difficulty of figuring this kind of problem out when doing my SOAP book. I wrote a utility that lets you trap both sides of the client-server text stream and even alter and resend messages. With that you can see exactly what the server is sending back. You can download it from a link at the bottom of this page. Bill
hi Kyle/William I tried a different method using HTTP Post to get the file across and was coming up with a similar problem. This time I converted the file to a byte array before sending it. Unfotunately the same error was happening. I resolved this problem by taking on board what you suggested Kyle which was to encode the byte array to base64 before sending it. And it worked! I suspect but haven't tried yet that this is the same problem that is happening with the JAXM SOAP connection. When I've tried it I'll let you know. Its strange that this isn't documented anywhere. I would have thought it encodes it automatically for you? I guess though that most of this sort of thing is done direct from a JSP which encodes it automatically??? Thanks for your help...