当然,除了使用DataHandler来创建SOAP消息外,还可以按照前面介绍的使用基本的IO的方式来创建。
下面来看客户端的代码。如例程14所示。
package com.hellking.webservice; import javax.xml.soap.*; import java.io.*; import java.awt.image.*; import java.awt.*; import com.sun.image.codec.jpeg.*; import javax.swing.JFrame; public class GetImageByAttachmentClient extends ImageClient { String endPointURLString = "http://localhost:8080/axis/servlet/GetImageByAttachment"; public static void main(String[] args)throws Exception { new GetImageByAttachmentClient(); } public Image getImageF
java webservice 传输文件(五)
MessageFactory.newInstance(); SOAPMessage soapMessage = messageFactory.createMessage(); SOAPPart soapPart = soapMessage.getSOAPPart(); SOAPEnvelope requestEnvelope = soapPart.getEnvelope(); SOAPBody body = requestEnvelope.getBody(); SOAPBodyElement operation = body.addBodyElement (requestEnvelope.createName("image")); //创建附件 URL url = new URL("http://localhost:8080/axis/test.jpg");//图像文件的储存的位置 DataHandler dh = new DataHandler(url); AttachmentPart attachment2 = message.createAttachmentPart(dh); attachment2.setContentId("myImage");//使用JAF来创建SOAP mime附件 soapMessage.addAttachmentPart(attachment2); soapMessage.writeTo(System.out); soapMessage.saveChanges(); return soapMessage; } catch(Exception ex) { ex.printStackTrace(); return null; } } }
romWebservice() { try { //创建SOAP消息 SOAPConnectionFactory soapConnectionFactory = javax.xml.soap.SOAPConnectionFactory.newInstance(); SOAPConnection soapConnection = soapConnectionFactory.createConnection(); MessageFactory messageFactory = MessageFactory.newInstance(); SOAPMessage soapMessage = messageFactory.createMessage(); SOAPPart soapPart = soapMessage.getSOAPPart(); SOAPEnvelope requestEnvelope = soapPart.getEnvelope(); SOAPBody body = requestEnvelope.getBody(); SOAPBodyElement operation = body.addBodyElement (requestEnvelope.createName("getImage")); javax.xml.soap.SOAPElement element = operation.addChildElement(requestEnvelope.createName("test")); operation.addChildElement("testgetImage").addTextNode("getImagebyAttachment_Test"); soapMessage.writeTo(System.out); //使用JAXM进行调用 javax.xml.soap.SOAPMessage returnedSOAPMessage = soapConnection.call(soapMessage, endPointURLString); System.out.println("========"); returnedSOAPMessage.writeTo(System.out);//打印返回的SOAP消息 System.out.println("========"); java.util.Iterator it = returnedSOAPMessage.getAttachments(); BufferedImage image=null ; //获得附件,逐个对附件进行处理 while (it.hasNext()) { AttachmentPart attachment = (AttachmentPart)it.next(); printAttachmentInfo(attachment); image= decodeImage(a