日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

通向架构师的道路(第十二天)之Axis2 Web Service(三)

發布時間:2024/4/14 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 通向架构师的道路(第十二天)之Axis2 Web Service(三) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、SOAPIn Axis2

在前兩天的教程中,我們學習到了用Axis2如何進行復雜數據、簡單數據進行傳輸。

正如我在前一天教程中所說,在web service的世界里,一切都是基于SOAP的,因此在今天我們將學習Axis2中的SOAP特性。

今天的課程將用3個例子來完成即:

1)? 客戶端與服務端使用SOAP進行通訊

2)? 服務端將Exception以SOAPFault的形式拋給客戶端

3)? 使用SWA(Soap With Attachment)來進行附件傳送

二、客戶端與服務端使用SOAP進行通訊

來看下面這個Web Service:

下面是Service端的源碼

org.sky.axis2.soap.SoapService

package org.sky.axis2.soap;

import org.apache.axiom.om.OMAbstractFactory;

import org.apache.axiom.om.OMElement;

import org.apache.axiom.om.OMFactory;

import org.apache.axiom.om.OMNamespace;

import?Java.util.*;

public class SoapService {

???????? public static OMElement requestSoap = null;

???????? public OMElement request(OMElement soapBody) {

?????????????????? requestSoap = soapBody;

?????????????????? Iterator it = requestSoap.getChildElements();

OMElement issuerElement = (OMElement) it.next();

?????????????????? OMElement serialElement = (OMElement) it.next();

?????????????????? OMElement revocationDateElement = (OMElement) it.next();

?????????????????? String issuer = issuerElement.getText();

?????????????????? String serial = serialElement.getText();

?????????????????? String revocationDate = revocationDateElement.getText();

?????????????????? System.out.println("issuer=====" + issuer);

?????????????????? System.out.println("serial=====" + serial);

?????????????????? System.out.println("revocationDate=====" + revocationDate);

?????????????????? OMFactory soapFactory = OMAbstractFactory.getOMFactory();

?????????????????? OMNamespace omNs = soapFactory.createOMNamespace(

???????????????????????????????????? "http://soap.axis2.sky.org", "");

?????????????????? OMElement soapResponse = soapFactory.createOMElement("SoapResponse",

???????????????????????????????????? omNs);

???????????????????OMElement soapIssuer = soapFactory.createOMElement("Issuer", omNs);

?????????????????? soapIssuer.setText("issuer: " + issuer);

?????????????????? soapResponse.addChild(soapIssuer);

?????????????????? OMElement soapSerial = soapFactory.createOMElement("Serial", omNs);

?????????????????? soapSerial.setText("serial: " + serial);

?????????????????? soapResponse.addChild(soapSerial);

?????????????????? OMElement soapRevokeDate = soapFactory.createOMElement("RevokeDate",

???????????????????????????????????? omNs);

?????????????????? soapRevokeDate.setText("RevocationDate: " + revocationDate);

?????????????????? soapResponse.addChild(soapRevokeDate);

???????????????????soapResponse.build();

?????????????????? return soapResponse;

???????? }

}

來看它的service.xml的描述

<service name="SoapService">

???????? <description>

?????????????????? This is the service for revoking certificate.

???????? </description>

???????? <parameter name="ServiceClass" locked="false">

?????????????????? org.sky.axis2.soap.SoapService

???????? </parameter>

???????? <operation name="request">

?????????????????? <messageReceiver

??????????????????????????? class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver" />

?????????????????? <actionMapping>urn:request</actionMapping>

???????? </operation>

</service>

該Web Service接受一個Soap請求,該請求為如下格式:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://soap.axis2.sky.org">

?? <soapenv:Header/>

?? <soapenv:Body>

????? <soap:request>

?????????? <soap:request>?</soap:request>

????? </soap:request>

?? </soapenv:Body>

</soapenv:Envelope>

其中<soap:request></soap:request>中間的內容,應該如下所示:

<Request xmlns="http://10.225.104.122">

??? <Issuer>1234567890</Issuer>

??? <Serial>11111111</Serial>

??? <RevokeDate>2007-01-01</RevokeDate>

</ Response >

我們假設它是一個購買圖書的定單,服務端收到這個請求后會返回一個定單信息給調用它的客戶端,服務端將返回如下內容(此處不做任何業務處理,只是很簡單的傳值回客戶端)。

<SoapResponse xmlns="http://soap.axis2.sky.org">

??? <Issuer>issuer: Wrox</Issuer>

??? <Serial>serial: 1111111111ISBN</Serial>

??? <RevokeDate>RevocationDate: 2012-07-29</RevokeDate>

</SoapResponse>

為生成上述這個SoapResponse我們在Service端的核心代碼如上面加粗部分的代碼所示,由其注意這個“soapResponse.build();”。

下面我們來看這個客戶端是怎么寫的,我們這邊用的是非阻塞式客戶端

org.sky.axis2.soap.SoapServiceClient

package org.sky.axis2.soap;

import org.apache.axiom.om.OMAbstractFactory;

import org.apache.axiom.om.OMElement;

import org.apache.axiom.om.OMFactory;

import org.apache.axiom.om.OMNamespace;

import org.apache.axis2.AxisFault;

import org.apache.axis2.Constants;

import org.apache.axis2.addressing.EndpointReference;

import org.apache.axis2.client.Options;

import org.apache.axis2.client.ServiceClient;

import org.apache.axis2.client.async.AxisCallback;

import org.apache.axis2.context.MessageContext;

import javax.xml.namespace.QName;

public class SoapServiceClient {

???????? private static EndpointReference targetEPR = new EndpointReference(

??????????????????????????? "http://localhost:8080/Axis2Service/services/SoapService");

???????? public static boolean finish = false;

???????? public static void orderRequest() {

?????????????????? OMFactory factory = OMAbstractFactory.getOMFactory();

???????????????????OMNamespace omNs = factory.createOMNamespace(

?????????????????????????????????????"http://soap.axis2.sky.org", "");

???????????????????OMElement issuer = factory.createOMElement("Issuer", omNs);

???????????????????OMElement serial = factory.createOMElement("Serial", omNs);

???????????????????OMElement revocationDate = factory.createOMElement("RevocationDate",

?????????????????????????????????????omNs);

???????????????????issuer.setText("Wrox");

???????????????????serial.setText("1111111111ISBN");

???????????????????revocationDate.setText("2012-07-29");

???????????????????OMElement requestSoapMessage = factory.createOMElement("request", omNs);

???????????????????requestSoapMessage.addChild(issuer);

???????????????????requestSoapMessage.addChild(serial);

???????????????????requestSoapMessage.addChild(revocationDate);

???????????????????requestSoapMessage.build();

?????????????????? Options options = new Options();

?????????????????? options.setTo(targetEPR);

?????????????????? ServiceClient sender = null;

?????????????????? try {

??????????????????????????? AxisCallback callback = new AxisCallback() {

???????????????????????????????????? public void onMessage(MessageContext msgContext) {

???????????????????????????????????????????????OMElement result = msgContext.getEnvelope().getBody()

????????????????????????????????????????????????????????????????.getFirstElement();

?????????????????????????????????????????????? // System.out.println(msgContext.toString());

?????????????????????????????????????????????? // System.out.println(msgContext.getEnvelope().toString());

???????????????????????????????????????????????System.out.println(msgContext.getEnvelope().getBody()

????????????????????????????????????????????????????????????????.getFirstElement());

??????????????????????????????????????????????finish = true;

???????????????????????????????????? }

???????????????????????????????????? public void onFault(MessageContext msgContext) {

?????????????????????????????????????????????? System.out.println(msgContext.getEnvelope().getBody()

???????????????????????????????????????????????????????????????? .getFault().toString());

???????????????????????????????????? }

???????????????????????????????????? public void onError(Exception e) {

???????????????????????????????????? }

???????????????????????????????????? public void onComplete() {

?????????????????????????????????????????????? System.out.println("Completed!!!");

???????????????????????????????????? }

??????????????????????????? };

??????????????????????????? sender = new ServiceClient();

??????????????????????????? sender.setOptions(options);

??????????????????????????? System.out.println("-------Invoke the service---------");

??????????????????????????? sender.sendReceiveNonBlocking(requestSoapMessage, callback);

??????????????????????????? synchronized (callback) {

???????????????????????????????????? if (!finish) {

?????????????????????????????????????????????? try {

??????????????????????????????????????????????????????? callback.wait(1000);

?????????????????????????????????????????????? } catch (Exception e) {

?????????????????????????????????????????????? }

???????????????????????????????????? }

???????????????????????????????????? if (!finish) {

?????????????????????????????????????????????? throw new AxisFault(

???????????????????????????????????????????????????????????????? "Server was shutdown as the async response take too long to complete");

???????????????????????????????????? }

??????????????????????????? }

?????????????????? } catch (AxisFault e) {

??????????????????????????? e.printStackTrace();

?????????????????? } finally {

??????????????????????????? if (sender != null)

???????????????????????????????????? try {

?????????????????????????????????????????????? sender.cleanup();

???????????????????????????????????? } catch (Exception e) {

???????????????????????????????????? }

?????????????????? }

???????? }

???????? public static void main(String[] args) {

?????????????????? orderRequest();

???????? }

}

上述代碼和前兩天的客戶端代碼沒啥區別,我已經把核心代碼用紅色給標粗了。

運行后行得到輸出

客戶端運行后的輸出:

服務端的輸出:

三、服務端將Exception以SOAPFault的形式拋給客戶端

上面這個例子很簡單,它展示了一個客戶端向服務端發送一個request,服務端接收到客戶端的Request(OMElement類型)后解析并根據相應的業務邏輯向客戶端再返回一個response(OMElement類型)的完整過程。

下面我們要來看的是,如果客戶端在調用服務器時發生任何錯誤,服務端如何把這個錯誤經過包裝后再返回給客戶端的例子。

還記得我們在非阻塞式客戶端中有如下這樣的觸發器嗎?

public void onMessage(MessageContext msgContext) {

}

public void onFault(MessageContext msgContext) {

}

public void onError(Exception e) {

}

public void onComplete() {

}

此處的onFault就是用于接受從服務端拋過來的Exception的,我們把它稱為SOAPFault。

下面來看一個例子,先來看Service端

org.sky.axis2.soap.SoapFaultService

package org.sky.axis2.soap;

import org.apache.axiom.om.OMAbstractFactory;

import org.apache.axiom.om.OMElement;

import org.apache.axiom.om.OMFactory;

import org.apache.axiom.om.OMNamespace;

import org.apache.axiom.soap.SOAPFactory;

import org.apache.axiom.soap.SOAPFault;

import org.apache.axiom.soap.SOAPFaultCode;

import org.apache.axiom.soap.SOAPFaultReason;

import org.apache.axis2.AxisFault;

import org.apache.axis2.context.MessageContext;

public class SoapFaultService {

???????? private int i = 0;

???????? public OMElement getPrice(OMElement request) throws AxisFault {

?????????????????? if (request == null) {

??????????????????????????? SOAPFault fault = getSOAPFault();

??????????????????????????? return fault;

?????????????????? }

?????????????????? OMFactory factory = OMAbstractFactory.getOMFactory();

?????????????????? OMNamespace ns = factory.createOMNamespace("", "");

?????????????????? OMElement response = factory.createOMElement("Price", ns);

?????????????????? response.setText(String.valueOf(i++));

?????????????????? return response;

???????? }

???????? private SOAPFault getSOAPFault() {

?????????????????? MessageContext context = MessageContext.getCurrentMessageContext();

?????????????????? SOAPFactory factory = null;

?????????????????? if (context.isSOAP11()) {

??????????????????????????? factory = OMAbstractFactory.getSOAP11Factory();

?????????????????? } else {

??????????????????????????? factory = OMAbstractFactory.getSOAP12Factory();

?????????????????? }

???????????????????SOAPFault fault = factory.createSOAPFault();

???????????????????SOAPFaultCode faultCode = factory.createSOAPFaultCode(fault);

?????????????????? faultCode.setText("13");

?????????????????? factory.createSOAPFaultValue(faultCode);

?????????????????? SOAPFaultReason faultReason = factory.createSOAPFaultReason(fault);

?????????????????? faultReason.setText("request can not be null");

?????????????????? factory.createSOAPFaultText(faultReason);

?????????????????? factory.createSOAPFaultDetail(fault);

?????????????????? return fault;

???????? }

}

注意加粗部分的代碼,由其是標成紅色的代碼為核心代碼。

來看Service描述:

<service name="SoapFaultService">

???????? <Description>

?????????????????? Please Type your service description here

???????? </Description>

???????? <parameter name="ServiceClass" locked="false">org.sky.axis2.soap.SoapFaultService

???????? </parameter>

???????? <operation name="getPrice">

?????????????????? <messageReceiver

??????????????????????????? class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver" />

?????????????????? <actionMapping>urn:getPrice</actionMapping>

???????? </operation>

</service>

上述這個WebService接受一個輸入的參數,如果輸入的內容為空,則返回一個SoapFault,即鍵值為13,內容為” request can not be null”。

我們來看客戶端的代碼

org.sky.axis2.soap.SoapFaultClient

package org.sky.axis2.soap;

import java.util.Iterator;

import javax.xml.namespace.QName;

import org.apache.axiom.om.OMAbstractFactory;

import org.apache.axiom.om.OMElement;

import org.apache.axiom.om.OMFactory;

import org.apache.axiom.om.OMNamespace;

import org.apache.axis2.AxisFault;

import org.apache.axis2.Constants;

import org.apache.axis2.addressing.EndpointReference;

import org.apache.axis2.client.Options;

import org.apache.axis2.client.ServiceClient;

import org.apache.axis2.client.async.AxisCallback;

import org.apache.axis2.context.MessageContext;

public class SoapFaultClient {

???????? static boolean finish = false;

???????? public static void main(String[] args) {

?????????????????? EndpointReference epr = new EndpointReference(

?????????????????? "http://localhost:8080/Axis2Service/services/SoapFaultService");

?????????????????? ServiceClient sender = null;

?????????????????? try {

??????????????????????????? OMFactory factory = OMAbstractFactory.getOMFactory();

??????????????????????????? OMNamespace ns = factory.createOMNamespace(

?????????????????????????????????????????????? "http://soap.axis2.sky.org", "");

??????????????????????????? OMElement request = factory.createOMElement("Price", ns);

??????????????????????????? Options options = new Options();

??????????????????????????? options.setAction("urn:getPrice");

??????????????????????????? options.setTo(epr);

??????????????????????????? options.setTransportInProtocol(Constants.TRANSPORT_HTTP);

??????????????????????????? options.setUseSeparateListener(true);

??????????????????????????? AxisCallback callback = new AxisCallback() {

???????????????????????????????????? public void onMessage(MessageContext msgContext) {

?????????????????????????????????????????????? OMElement result = msgContext.getEnvelope().getBody()

???????????????????????????????????????????????????????????????? .getFirstElement();

?????????????????????????????????????????????? OMElement priceElement = result;

?????????????????????????????????????????????? System.out.println("price====" + priceElement.getText());

?????????????????????????????????????????????? finish = true;

???????????????????????????????????? }

?????????????????????????????????????public void onFault(MessageContext msgContext) {

??????????????????????????????????????????????QName errorCode = new QName("faultcode");

??????????????????????????????????????????????QName reason = new QName("faultstring");

??????????????????????????????????????????????// System.out.println("on

??????????????????????????????????????????????// fault:"+msgContext.getEnvelope().getBody().getFault().toString());

??????????????????????????????????????????????OMElement fault = msgContext.getEnvelope().getBody()

????????????????????????????????????????????????????????????????.getFault();

??????????????????????????????????????????????System.out.println("ErrorCode["

????????????????????????????????????????????????????????????????+ fault.getFirstChildWithName(errorCode).getText()

????????????????????????????????????????????????????????????????+ "] caused by: "

????????????????????????????????????????????????????????????????+ fault.getFirstChildWithName(reason).getText());

?????????????????????????????????????}

???????????????????????????????????? public void onError(Exception e) {

???????????????????????????????????? }

???????????????????????????????????? public void onComplete() {

?????????????????????????????????????????????? System.out.println("OnComplete!!!");

???????????????????????????????????? }

??????????????????????????? };

??????????????????????????? sender = new ServiceClient();

??????????????????????????? sender.setOptions(options);

??????????????????????????? sender.engageModule("addressing");

??????????????????????????? try {

???????????????????????????????????? // sender.sendReceiveNonBlocking(request, callback);

???????????????????????????????????? sender.sendReceiveNonBlocking(null, callback);

??????????????????????????? } catch (AxisFault e) {

???????????????????????????????????? System.out.println("Exception occur!");

???????????????????????????????????? System.out.println(e.getMessage());

??????????????????????????? }

??????????????????????????? synchronized (callback) {

???????????????????????????????????? if (!finish) {

?????????????????????????????????????????????? try {

??????????????????????????????????????????????????????? callback.wait(1000);

?????????????????????????????????????????????? } catch (Exception e) {

?????????????????????????????????????????????? }

???????????????????????????????????? }

??????????????????????????? }

?????????????????? } catch (AxisFault e) {

??????????????????????????? e.printStackTrace();

??????????????????????????? System.out.println(e.getMessage());

?????????????????? } finally {

??????????????????????????? try {

???????????????????????????????????? sender.cleanup();

??????????????????????????? } catch (Exception e) {

??????????????????????????? }

?????????????????? }

???????? }

}

注意紅色并加粗部分的代碼,為了抓到服務端拋過來的SoapFault我們必須使用非阻塞式,因此我們在onFault處,進行接受服務端錯誤的處理。

注意:

我們調用Service端時沒有傳入Service端所需要的request的參數:

// sender.sendReceiveNonBlocking(request,callback);

sender.sendReceiveNonBlocking(null,callback);

這將構成Service端拋出SoapFault。

來看運行效果:

四、使用SWA(Soap WithAttachment)來進行附件傳送

有了上面兩個例子的基礎后,我們將使用這個例子來結束Axis2中的Soap特性的教學。

在Axis2中傳輸附件有兩種形式,一種叫MTOM,一種就是SWA。

SWAP即Soap With Attachment,這是業界的標準。

所謂的SWA傳輸,即客戶端把需要上傳的文件,編譯成兩進制代碼凌晨隨著soap的request一起推送到服務端,該兩進制代碼以AttachmentId的形式來表示,即如下這樣的一個soap body:

<soapenv:Body>

<uploadFile xmlns="http://attachment.axis2.sky.org">

<name>test.jpg</name>

<attchmentID>urn:uuid:8B43A26FEE1492F85A1343628038693</attchmentID>

</uploadFile>

</soapenv:Body>

服務端收到該soap的request可以直接使用如下的語句將這個AttachmentId還原成輸出流:

DataHandler dataHandler = attachment.getDataHandler(attchmentID);

File file = new File(uploadFilePath.toString());

fileOutputStream = new FileOutputStream(file);

dataHandler.writeTo(fileOutputStream);

fileOutputStream.flush();

在我們這個例子內,我們將使用客戶端上傳一個jpg文件,服務端收到該jpg文件(可以是任何的兩進制文件)后解析后存入服務端的一個目錄。

先來看服務端代碼

org.sky.axis2.attachment.FileUploadService

package org.sky.axis2.attachment;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import javax.activation.DataHandler;

import org.apache.axiom.attachments.Attachments;

import org.apache.axis2.context.MessageContext;

import org.sky.axis2.util.UUID;

public class FileUploadService {

???????? public String uploadFile(String name, String attchmentID) throws Exception {

?????????????????? FileOutputStream fileOutputStream = null;

?????????????????? StringBuffer uploadFilePath = new StringBuffer();

?????????????????? String fileNamePrefix = "";

?????????????????? String fileName = "";

?????????????????? try {

????????????????????????????MessageContext msgCtx = MessageContext.getCurrentMessageContext();

????????????????????????????Attachments attachment = msgCtx.getAttachmentMap();

????????????????????????????DataHandler dataHandler = attachment.getDataHandler(attchmentID);

????????????????????????????fileNamePrefix = name.substring(name.indexOf("."), name.length());

????????????????????????????fileName = UUID.getUUID();

????????????????????????????System.out.println("fileName=====" + fileName);

????????????????????????????System.out.println("fileNamePrefix====" + fileNamePrefix);

????????????????????????????uploadFilePath.append("D:/upload/axis2/");

????????????????????????????uploadFilePath.append(fileName);

????????????????????????????uploadFilePath.append(fileNamePrefix);

????????????????????????????System.out

??????????????????????????????????????????????.println("uploadFilePath====" + uploadFilePath.toString());

????????????????????????????File file = new File(uploadFilePath.toString());

????????????????????????????fileOutputStream = new FileOutputStream(file);

????????????????????????????dataHandler.writeTo(fileOutputStream);

????????????????????????????fileOutputStream.flush();

?????????????????? } catch (Exception e) {

??????????????????????????? throw new Exception(e);

?????????????????? } finally {

??????????????????????????? try {

???????????????????????????????????? if (fileOutputStream != null) {

?????????????????????????????????????????????? fileOutputStream.close();

?????????????????????????????????????????????? fileOutputStream = null;

???????????????????????????????????? }

??????????????????????????? } catch (Exception e) {

??????????????????????????? }

?????????????????? }

?????????????????? return "File saved succesfully.";

???????? }

}

下面是服務端的描述

service.xml文件的內容為:

<service name="AttachmentService">

???????? <parameter name="ServiceClass">org.sky.axis2.attachment.FileUploadService

???????? </parameter>

???????? <operation name="uploadFile">

?????????????????? <actionMapping>urn:uploadFile</actionMapping>

?????????????????? <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />

???????? </operation>

</service>

該服務端接受客戶端上傳的附件后使用UUID重新命名上傳的文件名,并將其存入服務端的” D:/upload/axis2/”目錄中。

來看客戶端代碼

org.sky.axis2.attachment.FileUploadClient

package org.sky.axis2.attachment;

import java.io.File;

import javax.activation.DataHandler;

import javax.activation.FileDataSource;

import javax.xml.namespace.QName;

import org.apache.axiom.om.OMAbstractFactory;

import org.apache.axiom.om.OMElement;

import org.apache.axiom.om.OMNamespace;

import org.apache.axiom.soap.SOAP11Constants;

import org.apache.axiom.soap.SOAPBody;

import org.apache.axiom.soap.SOAPEnvelope;

import org.apache.axiom.soap.SOAPFactory;

import org.apache.axis2.Constants;

import org.apache.axis2.addressing.EndpointReference;

import org.apache.axis2.client.OperationClient;

import org.apache.axis2.client.Options;

import org.apache.axis2.client.ServiceClient;

import org.apache.axis2.context.ConfigurationContext;

import org.apache.axis2.context.ConfigurationContextFactory;

import org.apache.axis2.context.MessageContext;

import org.apache.axis2.wsdl.WSDLConstants;

public class FileUploadClient {

???????? private static EndpointReference targetEPR = new EndpointReference(

??????????????????????????? "http://localhost:8080/Axis2Service/services/AttachmentService");

???????? public static void main(String[] args) throws Exception {

?????????????????? new FileUploadClient().transferFile();

???????? }

???????? public void transferFile() throws Exception {

?????????????????? String filePath = "D:/deployment/test.jpg";

?????????????????? String destFile = "test.jpg";

?????????????????? Options options = new Options();

?????????????????? options.setTo(targetEPR);

???????????????????options.setProperty(Constants.Configuration.ENABLE_SWA,

?????????????????????????????????????Constants.VALUE_TRUE);

???????????????????options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);

?????????????????? options.setTimeOutInMilliSeconds(10000);

?????????????????? options.setTo(targetEPR);

?????????????????? options.setAction("urn:uploadFile");

???????????????????ConfigurationContext configContext = ConfigurationContextFactory

?????????????????????????????????????.createConfigurationContextFromFileSystem(

???????????????????????????????????????????????????????"D:/wspace/Axis2Service/WebContent/WEB-INF/modules",

???????????????????????????????????????????????????????null);

?????????????????? ServiceClient sender = new ServiceClient(configContext, null);

?????????????????? sender.setOptions(options);

???????????????????OperationClient mepClient = sender

?????????????????????????????????????.createClient(ServiceClient.ANON_OUT_IN_OP);

???????????????????MessageContext mc = new MessageContext();

???????????????????FileDataSource fileDataSource = new FileDataSource(new File(filePath));

?????????????????? // Create a dataHandler using the fileDataSource. Any implementation of

?????????????????? // javax.activation.DataSource interface can fit here.

???????????????????DataHandler dataHandler = new DataHandler(fileDataSource);

???????????????????String attachmentID = mc.addAttachment(dataHandler);

?????????????????? SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();

?????????????????? SOAPEnvelope env = fac.getDefaultEnvelope();

?????????????????? OMNamespace omNs = fac.createOMNamespace(

???????????????????????????????????? "http://attachment.axis2.sky.org", "");

?????????????????? OMElement uploadFile = fac.createOMElement("uploadFile", omNs);

?????????????????? OMElement nameEle = fac.createOMElement("name", omNs);

?????????????????? nameEle.setText(destFile);

???????????????????OMElement idEle = fac.createOMElement("attchmentID", omNs);

???????????????????idEle.setText(attachmentID);

???????????????????uploadFile.addChild(nameEle);

???????????????????uploadFile.addChild(idEle);

???????????????????env.getBody().addChild(uploadFile);

?????????????????? System.out.println("message====" + env);

?????????????????? mc.setEnvelope(env);

?????????????????? mepClient.addMessageContext(mc);

?????????????????? mepClient.execute(true);

?????????????????? MessageContext response = mepClient

???????????????????????????????????? .getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);

?????????????????? SOAPBody body = response.getEnvelope().getBody();

?????????????????? OMElement element = body.getFirstElement().getFirstChildWithName(

???????????????????????????????????? new QName("http://attachment.axis2.sky.org", "return"));

?????????????????? System.out.println(element.getText());

???????? }

}

注意紅色加粗部分的代碼,由其是:

FileDataSource fileDataSource = new FileDataSource(new File(filePath));

String attachmentID = mc.addAttachment(dataHandler);

這兩句就是把客戶端需要上傳的附件轉成AttachmentId的語句,然后把這個AttachementId作為一個OMElement的類型加入到客戶端的soap request中去即可:

OMElement idEle = fac.createOMElement("attchmentID", omNs);

idEle.setText(attachmentID);

uploadFile.addChild(nameEle);

uploadFile.addChild(idEle);

env.getBody().addChild(uploadFile);

來看運行效果。

客戶端:

上傳d:/deployment/test.jpg文件

客戶端收到服務端返回的”File saved successfully”即可在服務端的” D:/upload/axis2”目錄中查詢是否成功上傳了該文件了

可以看到,由于我們使用的是UUID因此每次上傳,服務端的文件名都不會重復。

附錄 UUID.java

package org.sky.axis2.util;

public class UUID {

???????? protected static int count = 0;

???????? public static synchronized String getUUID() {

?????????????????? count++;

?????????????????? long time = System.currentTimeMillis();

?????????????????? String timePattern = Long.toHexString(time);

?????????????????? int leftBit = 14 - timePattern.length();

?????????????????? if (leftBit > 0) {

??????????????????????????? timePattern = "0000000000".substring(0, leftBit) + timePattern;

?????????????????? }

?????????????????? String uuid = timePattern

???????????????????????????????????? + Long.toHexString(Double.doubleToLongBits(Math.random()))

???????????????????????????????????? + Long.toHexString(Double.doubleToLongBits(Math.random()))

???????????????????????????????????? + "000000000000000000";

?????????????????? uuid = uuid.substring(0, 32).toUpperCase();

?????????????????? return uuid;

???????? }

}

總結

以上是生活随笔為你收集整理的通向架构师的道路(第十二天)之Axis2 Web Service(三)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲一区在线免费观看 | 欧美无砖区 | 亚洲视频四区 | 国产日韩一区 | 性视频在线 | 午夜黄色网 | 最近中文字幕在线免费观看 | 欧美人体一区二区 | 国产精品亚洲综合 | 女人被狂躁c到高潮喷水电影 | 欧美日韩123 | 女生被男生c | 免费成人美女在线观看. | 国产毛片电影 | 午夜寂寞福利 | 亚洲成人网在线播放 | 欧美在线性爱视频 | 久久毛片| 国产在线网站 | 欧美激情免费在线观看 | 夜夜骑夜夜操 | 好吊妞视频这里只有精品 | 黄色片网战| 成人在线观看免费高清 | 91看片视频 | 亚一区二区 | 性一交一乱一区二区洋洋av | 天堂在线精品视频 | 欧美xxxxx视频| 国产一二三四五区 | 男女午夜免费视频 | 国产福利一区二区 | 久久色中文字幕 | 91精品婷婷国产综合久久蝌蚪 | av网站免费看 | 污污av | 成人精品视频网站 | 色五五月| 欧美亚洲日本在线 | 日本在线不卡一区 | 伊人热久久 | 久久久精品亚洲 | 9l视频自拍九色9l视频 | 91久久精品www人人做人人爽 | 亚州福利 | 国产精品白嫩白嫩大学美女 | 自拍欧美日韩 | 美国色视频 | 人妖一区| 日韩欧美国产精品 | 黄色网址视频在线观看 | 午夜在线视频免费观看 | 女优中文字幕 | 久久久久亚洲日日精品 | 国产美女被草 | 性日本xxx | 男女精品视频 | 玉米地疯狂的吸允她的奶视频 | 在线一区二区三区视频 | 99久久伊人 | 亚洲 成人 av | 麻豆社| 蜜桃av网站 | 日本黄在线观看 | 国产精品一区二区av | 亚洲无限看| 一级黄色录像免费观看 | 亚洲熟妇无码av在线播放 | 天天操bb | 免费观看a级片 | 欧美三级理论片 | 男女国产精品 | 成人激情视频网 | 久久2019 | 欧美大片aaa| 午夜伦伦 | 99视频在线看| 中国挤奶哺乳午夜片 | 亚洲夜夜爽 | 亚洲一区二区影视 | 在线看一区二区 | 一区av在线 | 国产成人av免费观看 | 女同av在线播放 | 国产一区二区三区免费观看 | 91视频黄色 | 日韩欧美高清在线观看 | 美女裸体网站久久久 | 精品国产91乱码一区二区三区 | 97久草| 亚洲国产福利 | 日本黄色片免费看 | 日本无遮羞调教打屁股网站 | 91视频a| 操她视频网站 | 国产真实乱偷精品视频 | 国产精品偷伦视频免费观看了 | 国产99免费| 久久b|