工作環(huán)境
    
    :IDE:Eclipse 3.1.2 
  
  
    
       jdk: jdk1.5.0_06
    
  
  
    
       Tomcat: apache-tomcat-5.5.15
    
  
  
    
       AXIS2:1.0(war
    
    版本和bin版本) 
  
  
    
  環(huán)境準備: 
  
  
    
      
        http://ws.apache.org/axis2/download/1_0/download.cgi
      
    
    去下載AXIS2的 
 
    
      Binary Distribution
    
    
       url: 
      
        
          http://apache.justdn.org/ws/axis2/1_0/axis2-std-1.0-bin.zip
        
      
    
    
      和
    
    
      war 
    
  
  
    
      Distribution url: 
      
        
          http://apache.justdn.org/ws/axis2/1_0/axis2-1.0-docs.zip
        
      
    
    
      。把這兩個文件解壓,比如解壓縮的后得目錄為
    
    
      C:/axis2-std-1.0-bin
    
    
      和
    
    
      C:/axis2.war.
    
    
      在
    
    
      Eclipse
    
    
      下通過菜單
    
    
      window—preferences…--Java—Build Path—User Libraries 
    
    
      新建一個
    
    
      user library,
    
    
      比如名字就叫
    
    
      axis2
    
    
      把
    
    
      C:/axis2-std-1.0-bin/lib
    
    
      下的所有
    
    
      jar
    
    
      文件包含進來。把
    
    
      axis2.war
    
    
      拷貝到
    
    
      %TOMCAT-HOME%/webapps
    
    
      下面。
    
  
  
    
      實現(xiàn)
    
    : 
  
  
    
    
    
      在
    
    
      Eclipse
    
    
      新建一個工程,裝了
    
    
      Eclipse tomcat plugin
    
    
      的就新建一個
    
    
      tomcat project
    
    
      好了,
    
    
       build path
    
    
      包含上面創(chuàng)建的
    
    
      user library:axis2.
    
    
      我的例子的場景是一個語音信箱系統(tǒng)的用戶上傳下載問候語文件(
    
    
      greeting)
    
    
      的,每個語音信箱系統(tǒng)的用戶擁有一個唯一的
    
    
      mailbox number, 
    
    
      問候語有不同的類型,比如忙的時候問候語,出差時候問候語,不同時段的問候語,問候語文件支持的類型有
    
    
      wav,mp3 
    
    
      等等。
    
  
  
    
      所以我的
    
    
      webservice
    
    
      要實現(xiàn)的
    
    
      2
    
    
      個功能就是
    
    
      upload, download.
    
  
  
    
      AXIS2
    
    
      的
    
    
      webservice
    
    
      發(fā)布的時候是打包成
    
    
      xxx.aar
    
    
      發(fā)布的,
    
    
      xxx.aar
    
    
      展開后的目錄結構為
    
  
  
    
      --
    
  
  
    
       --META-INF
    
  
  
    
       services.xml
    
  
  
    
       --
    
    
      包含
    
    
      server
    
    
      端實現(xiàn)的
    
    
      class(
    
    
      目錄跟
    
    
      package
    
    
      是一樣的結構
    
    
      )
    
  
  
    
      新建
    
    
      2
    
    
      個類:
    
    
      FileTransferClient.java, interopService.java.
    
    
      兩個類內容如下:
    
  
  
    
      FileTransferClient(
    
    
      調用
    
    
      webservice
    
    
      的客戶端代碼
    
    
      )
    
  
  
    
      package sample.mtom.interop.client;
    
  
  
    
      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.om.OMText;
    
  
  
    
      import org.apache.axiom.soap.SOAP11Constants;
    
  
  
    
      import org.apache.axiom.soap.SOAP12Constants;
    
  
  
    
      import org.apache.axis2.Constants;
    
  
  
    
      import org.apache.axis2.addressing.EndpointReference;
    
  
  
    
      import org.apache.axis2.client.Options;
    
  
  
    
      import org.apache.axis2.client.ServiceClient;
    
  
  
    
      import javax.activation.DataHandler;
    
  
  
    
      import javax.activation.FileDataSource;
    
  
  
    
      import javax.xml.namespace.QName;
    
  
  
    
      import java.io.File;
    
  
  
    
      import java.io.InputStream;
    
  
  
    
      public class FileTransferClient {
    
  
  
    
       private static EndpointReference targetEPR = new EndpointReference("http://127.0.0.1:8080/axis2/services/interop");
    
  
  
    
       public static boolean upload(String mailboxnum, short greetingType, File file, String fileType) {
    
  
  
    
       try {
    
  
  
    
       OMElement data = buildUploadEnvelope(mailboxnum, greetingType, file, fileType);
    
  
  
    
       Options options = buildOptions();
    
  
  
    
       ServiceClient sender = new ServiceClient();
    
  
  
    
       sender.setOptions(options);
    
  
  
    
       System.out.println(data);
    
  
  
    
       OMElement ome = sender.sendReceive(data);
    
  
  
    
       System.out.println(ome);
    
  
  
    
       String b = ome.getText();
    
  
  
    
       return Boolean.parseBoolean(b);
    
  
  
    
       }
    
  
  
    
       catch(Exception e) {
    
  
  
    
       }
    
  
  
    
       return false;
    
  
  
    
       }
    
  
  
    
       public static InputStream download(String mailboxnum, short greetingType, String FileType) {
    
  
  
    
       try {
    
  
  
    
       OMElement data = buildDownloadEnvelope(mailboxnum, greetingType, FileType);
    
  
  
    
       Options options = buildOptions();
    
  
  
    
       ServiceClient sender = new ServiceClient();
    
  
  
    
       sender.setOptions(options);
    
  
  
    
       System.out.println(data);
    
  
  
    
       OMElement ome = sender.sendReceive(data);
    
  
  
    
       System.out.println(ome);
    
  
  
    
       OMText binaryNode = (OMText) ome.getFirstOMChild();
    
  
  
    
       DataHandler actualDH = (DataHandler) binaryNode.getDataHandler();
    
  
  
    
       return actualDH.getInputStream();
    
  
  
    
       }
    
  
  
    
       catch(Exception e) {
    
  
  
    
       }
    
  
  
    
       return null;
    
  
  
    
       }
    
  
  
    
       private static OMElement buildUploadEnvelope(String mailboxnum, short greetingType, File file, String FileType) {
    
  
  
    
       DataHandler expectedDH;
    
  
  
    
       OMFactory fac = OMAbstractFactory.getOMFactory();
    
  
  
    
       OMNamespace omNs = fac.createOMNamespace("http://example.org/mtom/data", "x");
    
  
  
    
       OMElement data = fac.createOMElement("upload", omNs);
    
  
  
    
       OMElement fileContent = fac.createOMElement("fileContent", omNs);
    
  
  
    
       FileDataSource dataSource = new FileDataSource(file);
    
  
  
    
       expectedDH = new DataHandler(dataSource);
    
  
  
    
       OMText textData = fac.createOMText(expectedDH, true);
    
  
  
    
       fileContent.addChild(textData);
    
  
  
    
       OMElement mboxnum = fac.createOMElement("mailboxnum", omNs);
    
  
  
    
       mboxnum.setText(mailboxnum);
    
  
  
    
       OMElement gtType = fac.createOMElement("greetingType", omNs);
    
  
  
    
       gtType.setText(greetingType+"");
    
  
  
    
       OMElement fileType=fac.createOMElement("fileType", omNs);
    
  
  
    
       fileType.setText(FileType);
    
  
  
    
       data.addChild(mboxnum);
    
  
  
    
       data.addChild(gtType);
    
  
  
    
       data.addChild(fileType);
    
  
  
    
       data.addChild(fileContent);
    
  
  
    
       return data;
    
  
  
    
       }
    
  
  
    
       private static OMElement buildDownloadEnvelope(String mailboxnum, short greetingType, String FileType) {
    
  
  
    
       OMFactory fac = OMAbstractFactory.getOMFactory();
    
  
  
    
       OMNamespace omNs = fac.createOMNamespace("http://example.org/mtom/data", "x");
    
  
  
    
       OMElement data = fac.createOMElement("download", omNs);
    
  
  
    
       OMElement mboxnum = fac.createOMElement("mailboxnum", omNs);
    
  
  
    
       mboxnum.setText(mailboxnum);
    
  
  
    
       OMElement gtType = fac.createOMElement("greetingType", omNs);
    
  
  
    
       gtType.setText(greetingType+"");
    
  
  
    
       OMElement fileType=fac.createOMElement("fileType", omNs);
    
  
  
    
       fileType.setText(FileType);
    
  
  
    
       data.addChild(mboxnum);
    
  
  
    
       data.addChild(gtType);
    
  
  
    
       data.addChild(fileType);
    
  
  
    
       return data;
    
  
  
    
       }
    
  
  
    
       private static Options buildOptions() {
    
  
  
    
       Options options = new Options();
    
  
  
    
       options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
    
  
  
    
       options.setTo(targetEPR);
    
  
  
    
       // enabling MTOM in the client side
    
  
  
    
       options.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
    
  
  
    
       options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
    
  
  
    
       return options;
    
  
  
    
       }
    
  
  
    
       public static void main(String agrs[]) {
    
  
  
    
       String file = "C:/deploy.wsdd";
    
  
  
    
       short gt = 1;
    
  
  
    
       String mn = "20060405";
    
  
  
    
       String ft="wsdd";
    
  
  
    
       boolean rtv = upload(mn,gt,new File(file),ft);
    
  
  
    
       System.out.println(rtv);
    
  
  
    
       InputStream is = download(mn,gt,ft);
    
  
  
    
       }
    
  
  
    
      }
    
  
  
    
      interopService(webservice
    
    
      的
    
    
       server
    
    
      端實現(xiàn)代碼
    
    
      )
    
  
  
    
      package sample.mtom.interop.service;
    
  
  
    
      import org.apache.axiom.attachments.utils.IOUtils;
    
  
  
    
      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.om.OMText;
    
  
  
    
      import org.apache.axis2.AxisFault;
    
  
  
    
      import java.io.FileOutputStream;
    
  
  
    
      import java.io.*;
    
  
  
    
      import java.util.Iterator;
    
  
  
    
      import javax.activation.DataHandler;
    
  
  
    
      import javax.activation.FileDataSource;
    
  
  
    
      public class interopService {
    
  
  
    
      public static final String TMP_PATH = "c:/tmp";
    
  
  
    
      public OMElement upload(OMElement element) throws Exception {
    
  
  
    
       OMElement _fileContent = null;
    
  
  
    
       OMElement _mailboxnum = null;
    
  
  
    
       OMElement _greetingType = null;
    
  
  
    
       OMElement _fileType = null;
    
  
  
    
       System.out.println(element);
    
  
  
    
       for (Iterator _iterator = element.getChildElements(); _iterator.hasNext();) {
    
  
  
    
       OMElement _ele = (OMElement) _iterator.next();
    
  
  
    
       if (_ele.getLocalName().equalsIgnoreCase("fileContent")) {
    
  
  
    
       _fileContent = _ele;
    
  
  
    
       }
    
  
  
    
       if (_ele.getLocalName().equalsIgnoreCase("mailboxnum")) {
    
  
  
    
       _mailboxnum = _ele;
    
  
  
    
       }
    
  
  
    
       if (_ele.getLocalName().equalsIgnoreCase("greetingType")) {
    
  
  
    
       _greetingType = _ele;
    
  
  
    
       }
    
  
  
    
       if (_ele.getLocalName().equalsIgnoreCase("fileType")) {
    
  
  
    
       _fileType = _ele;
    
  
  
    
       }
    
  
  
    
       }
    
  
  
    
       if (_fileContent == null || _mailboxnum == null || _greetingType== null || _fileType==null) {
    
  
  
    
       throw new AxisFault("Either Image or FileName is null");
    
  
  
    
       }
    
  
  
    
       OMText binaryNode = (OMText) _fileContent.getFirstOMChild();
    
  
  
    
       String mboxNum = _mailboxnum.getText();
    
  
  
    
       String greetingType = _greetingType.getText();
    
  
  
    
       String fileType = _fileType.getText();
    
  
  
    
       String greetingstoreDir = TMP_PATH+"/"+mboxNum;
    
  
  
    
       File dir = new File(greetingstoreDir);
    
  
  
    
       if(!dir.exists()) {
    
  
  
    
       dir.mkdir();
    
  
  
    
       }
    
  
  
    
       String filePath = greetingstoreDir+"/"+greetingType+"."+fileType;
    
  
  
    
       File greetingFile = new File(filePath);
    
  
  
    
       if(greetingFile.exists()) {
    
  
  
    
       greetingFile.delete();
    
  
  
    
       greetingFile = new File(filePath);
    
  
  
    
       }
    
  
  
    
       // Extracting the data and saving
    
  
  
    
       DataHandler actualDH;
    
  
  
    
       actualDH = (DataHandler) binaryNode.getDataHandler();
    
  
  
    
       FileOutputStream imageOutStream = new FileOutputStream(greetingFile);
    
  
  
    
       InputStream is = actualDH.getInputStream();
    
  
  
    
       imageOutStream.write(IOUtils.getStreamAsByteArray(is));
    
  
  
    
       // setting response
    
  
  
    
       OMFactory fac = OMAbstractFactory.getOMFactory();
    
  
  
    
       OMNamespace ns = fac.createOMNamespace("http://example.org/mtom/data", "x");
    
  
  
    
       OMElement ele = fac.createOMElement("response", ns);
    
  
  
    
       ele.setText("true");
    
  
  
    
       return ele;
    
  
  
    
      }
    
  
  
    
      public OMElement download(OMElement element) throws Exception {
    
  
  
    
       System.out.println(element);
    
  
  
    
       OMElement _mailboxnum = null;
    
  
  
    
       OMElement _greetingType = null;
    
  
  
    
       OMElement _fileType = null;
    
  
  
    
       for (Iterator _iterator = element.getChildElements(); _iterator.hasNext();) {
    
  
  
    
       OMElement _ele = (OMElement) _iterator.next();
    
  
  
    
       if (_ele.getLocalName().equalsIgnoreCase("mailboxnum")) {
    
  
  
    
       _mailboxnum = _ele;
    
  
  
    
       }
    
  
  
    
       if (_ele.getLocalName().equalsIgnoreCase("greetingType")) {
    
  
  
    
       _greetingType = _ele;
    
  
  
    
       }
    
  
  
    
       if (_ele.getLocalName().equalsIgnoreCase("fileType")) {
    
  
  
    
       _fileType = _ele;
    
  
  
    
       }
    
  
  
    
       }
    
  
  
    
       String mboxNum = _mailboxnum.getText();
    
  
  
    
       String greetingType = _greetingType.getText();
    
  
  
    
       String fileType = _fileType.getText();
    
  
  
    
       String filePath = TMP_PATH+"/"+mboxNum+"/"+greetingType+"."+fileType;
    
  
  
    
       FileDataSource dataSource = new FileDataSource(filePath);
    
  
  
    
       DataHandler expectedDH = new DataHandler(dataSource);
    
  
  
    
       OMFactory fac = OMAbstractFactory.getOMFactory();
    
  
  
    
       OMNamespace ns = fac.createOMNamespace("http://example.org/mtom/data", "x");
    
  
  
    
       OMText textData = fac.createOMText(expectedDH, true);
    
  
  
    
       OMElement ele = fac.createOMElement("response", ns);
    
  
  
    
       ele.addChild(textData);
    
  
  
    
       return ele;
    
  
  
    
      }
    
  
  
    
      }
    
  
  
    
      新建一個
    
    
       services.xml,
    
    
      內容如下
    
    
      :
    
  
  
    
      <service
    
    
      name=
    
    
      "MTOMService"
    
    
      >
    
  
  
    
    
    
      <description>
    
  
  
    
       This is a sample Web Service with two operations,echo and ping.
    
  
  
    
    
    
      </description>
    
  
  
    
    
    
      <parameter
    
    
      name=
    
    
      "ServiceClass"
    
    
      locked=
    
    
      "false"
    
    
      >
    
    
      sample.mtom.interop.service.interopService
    
    
      </parameter>
    
  
  
    
    
    
      <operation
    
    
      name=
    
    
      "upload"
    
    
      >
    
  
  
    
    
    
      <actionMapping>
    
    
      urn:upload
    
    
      </actionMapping>
    
  
  
    
    
    
      <messageReceiver
    
    
      class=
    
    
      "org.apache.axis2.receivers.RawXMLINOutMessageReceiver"
    
    
      />
    
  
  
    
    
    
      </operation>
    
  
  
    
    
    
      <operation
    
    
      name=
    
    
      "download"
    
    
      >
    
  
  
    
    
    
      <actionMapping>
    
    
      urn:download
    
    
      </actionMapping>
    
  
  
    
    
    
      <messageReceiver
    
    
      class=
    
    
      "org.apache.axis2.receivers.RawXMLINOutMessageReceiver"
    
    
      />
    
  
  
    
    
    
      </operation>
    
  
  
    
      </service>
    
  
  
    
      打包
    
    : 
  
  
    
    
    
      新建一個目錄叫
    
    
      build,
    
    
      拷貝相應的文件到
    
    
      build
    
    
      下面,
    
    
      build
    
    
      的目錄展開如圖所示:
    
  
  
    
      就像這樣的
    
  
  
    
       --build
    
  
  
    
       --interop
    
  
  
    
       --META-INF
    
  
  
    
       --services.xml
    
  
  
    
       --sample
    
  
  
    
       --mtom
    
  
  
    
       --interop
    
  
  
    
      --service
    
  
  
    
       --
    
    
      interopService.class
    
  
  
    
      在
    
    
      windows
    
    
      環(huán)境下
    
    
      ,
    
    
      在
    
    
      console
    
    
      下,更換目錄到
    
    
      build/interop
    
    
      下
    
    
      :
    
  
  
    
      輸入一下命令:
    
  
  
    
  jar –cfinterop.aar . , 
 
    
      注意最后一個點代表當前目錄。
    
  
  
    
      
      
    
    
      發(fā)布
    
    : 
  
  
    
    
    
      AXIS2
    
    
      的
    
    
      webservice
    
    
      的發(fā)布是這樣的,首先你要先把
    
    
      AXIS2
    
    
      的
    
    
      war distribution
    
    
      發(fā)到到一個
    
    
      servlet
    
    
      容器中,我這里用的是
    
    
      Tomcat. 
    
    
      發(fā)布的方法前面提到了:
      
        把
      
    
    
      axis2.war
    
    
      拷貝到
    
    
      %TOMCAT-HOME%/webapps
    
    
      下
    
    
      。
    
  
  
    
      啟動
    
    
      tomcat,
    
    
      然后輸入:
    
    
      
        
          http://localhost:8080/axis2/axis2-admin/
        
      
    
    
      輸入用戶名
    
    
      admin 
    
    
      密碼
    
    
      axis2
    
    
      就進入了
    
    
      axis2 
    
    
      Web Admin Module,
    
    
      然后在頁面左邊的菜單的
    
    
      Tools
    
    
      下選擇
    
    
      Upload Service,
    
    
      然后選擇前面打好的
    
    
      interop.aar
    
  
  
    
      upload.
    
  
  
    
      還有種手動發(fā)布的方式
    
    
      ,
    
    
      你發(fā)布
    
    
      axis2.war 
    
    
      到
    
    
      tomcat
    
    
      的時候,
    
    
      tomcat
    
    
      會自動展開生成一個
    
    
      axis2
    
    
      的目錄在
    
  
  
    
      %TOMCAT-HOME%/webapps
    
    
      下面,進入到目錄
    
    
      %TOMCAT-HOME%/webapps/axis2/WEB-INF/services
    
    
      ,把前面打好包的
    
    
      interop.aar
    
    
      拷貝到這里
    
    
      .
    
  
  
    
      測試
    
    : 
  
  
    
    
    
      在
    
    
      eclipse
    
    
      里運行
    
    
      FileTransferClient
    
    
      。
    
  
  
    
      代碼分析
    
    : 
  
  
    
    
    
      利用
    
    
      Axis2
    
    
      的
    
    
      Mtom
    
    
      發(fā)送附件跟
    
    
      Sun
    
    
      的
    
    
      SAAJ
    
    
      差不多。用過
    
    
      javamail
    
    
      的朋友也會覺得代碼似曾相識,應為都應用了
    
    
      builder
    
    
      模式。要向一個
    
    
      webserive 
    
    
      發(fā)送請求,首先就要構建一個請求的
    
    
      Envelope,Axis2
    
    
      構建
    
    
      Envelope
    
    
      的時候是利用的
    
    
      Axis2
    
    
      的
    
    
      AXIOM api(
    
    
      就是
    
    
      axis2
    
    
      的
    
    
      java object
    
    
      和
    
    
      xml
    
    
      的映射處理機制
    
    
      ),
    
    
      其編程模式和
    
    
      DOM
    
    
      差不多的
    
    
      .
    
    
      看這一段
    
    
      :
    
  
  
    
      private static OMElement buildUploadEnvelope(String mailboxnum, short greetingType, File file, String FileType) {
    
  
  
    
       DataHandler expectedDH;
    
  
  
    
       OMFactory fac = OMAbstractFactory.getOMFactory();
    
  
  
    
       OMNamespace omNs = fac.createOMNamespace("http://example.org/mtom/data", "x");
    
  
  
    
       OMElement data = fac.createOMElement("upload", omNs);
    
  
  
    
       OMElement fileContent = fac.createOMElement("fileContent", omNs);
    
  
  
    
       FileDataSource dataSource = new FileDataSource(file);
    
  
  
    
       expectedDH = new DataHandler(dataSource);
    
  
  
    
       OMText textData = fac.createOMText(expectedDH, true);
    
  
  
    
       fileContent.addChild(textData);
    
  
  
    
       OMElement mboxnum = fac.createOMElement("mailboxnum", omNs);
    
  
  
    
       mboxnum.setText(mailboxnum);
    
  
  
    
       OMElement gtType = fac.createOMElement("greetingType", omNs);
    
  
  
    
       gtType.setText(greetingType+"");
    
  
  
    
       OMElement fileType=fac.createOMElement("fileType", omNs);
    
  
  
    
       fileType.setText(FileType);
    
  
  
    
       data.addChild(mboxnum);
    
  
  
    
       data.addChild(gtType);
    
  
  
    
       data.addChild(fileType);
    
  
  
    
       data.addChild(fileContent);
    
  
  
    
       return data;
    
  
  
    
       }
    
  
  
    
      這一段其實是構建的
    
    
      data
    
    
      對像是這樣一段
    
    
      xml
    
    
      的
    
    
      java object
    
    
      代表:
    
  
  
    
       <x:upload
    
    
      xmlns:x=
    
    
      "http://example.org/mtom/data"
    
    
      >
    
  
  
    
    
    
      <x:mailboxnum>
    
    
      20060405
    
    
      </x:mailboxnum>
    
  
  
    
    
    
      <x:greetingType>
    
    
      1
    
    
      </x:greetingType>
    
  
  
    
    
    
      <x:fileType>
    
    
      wsdd
    
    
      </x:fileType>
    
  
  
    
    
    
      <x:fileContent>
    
  
  
    
      Dwvc2VydmljZT4NCjwvZGVwbG95bWVudD4NCg0K
    
  
  
    
    
    
      </x:fileContent>
    
  
  
    
    
    
      </x:upload>
    
  
  
    
      其中的
    
    
      Dwvc2VydmljZT4NCjwvZGVwbG95bWVudD4NCg0K
    
    
      是要傳送的文件的內容代表,至于什么編碼,我沒有深究。注意這一句
    
    
      :
    
  
  
    
       OMElement data = fac.createOMElement("upload", omNs);
    
  
  
    
      這里的“
    
    
      upload
    
    
      ”
    
    
      參數(shù)對應的是
    
    
      webservice
    
    
      的一個操作的名稱,這個操作的名稱是跟
    
    
      webservice
    
    
      的
    
    
      server
    
    
      端實現(xiàn)類的方法名和
    
    
      services.xml
    
    
      的所定義的
    
  
  
    
      ]
    
    
      <operation
    
    
      name=
    
    
      "upload"
    
    
      >
    
  
  
    
    
    
      <actionMapping>
    
    
      urn:upload
    
    
      </actionMapping>
    
  
  
    
    
    
      <messageReceiver
    
    
      class=
    
    
      "org.apache.axis2.receivers.RawXMLINOutMessageReceiver"
    
    
      />
    
  
  
    
      </operation>
    
  
  
    
      要一致的。
    
  
  
    
      我們再看看這一段,
    
  
  
    
       private static Options buildOptions() {
    
  
  
    
       Options options = new Options();
    
  
  
    
       options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
    
  
  
    
       options.setTo(targetEPR);
    
  
  
    
       // enabling MTOM in the client side
    
  
  
    
       options.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
    
  
  
    
       options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
    
  
  
    
       return options;
    
  
  
    
       }
    
  
  
    
      這里構建的
    
    
      Options
    
    
      對象,顧名思義就是調用
    
    
      webservice
    
    
      的相應的選項:
    
  
  
    
      比如這里就指定了
    
    
      Soap
    
    
      協(xié)議為
    
    
      1.1 
    
    
      版
    
  
  
    
      指定了所請求的
    
    
      service 
    
    
      的
    
    
      EPR(
    
    
      就是地址
    
    
      )
    
    
      ,
    
  
  
    
    
    
      聲明在
    
    
      client
    
    
      應用
    
    
      MTOM
    
  
  
    
      指定傳輸協(xié)議為
    
    
      HTTP
    
  
  
    
      構建好要傳送的
    
    
      data
    
    
      和
    
    
      options
    
    
      后
    
    
      ,
    
    
      所執(zhí)行的代碼為:
    
  
  
    
       ServiceClient sender = new ServiceClient();
    
  
  
    
       //
    
    
      設定選項
    
  
  
    
       sender.setOptions(options);
    
  
  
    
       //
    
    
      打印要傳送的數(shù)據,為一段
    
    
      xml
    
  
  
    
       System.out.println(data);
    
  
  
    
       //
    
    
      傳送數(shù)據,得到返回值
    
  
  
    
       OMElement ome = sender.sendReceive(data);
    
  
  
    
       //
    
    
      打印返回值,為一段
    
    
      xml
    
  
  
    
       System.out.println(ome);
    
  
  
    
       //
    
    
      析取返回值中的數(shù)據
    
  
  
    
       String b = ome.getText();
    
  
  
    
       //
    
    
      返回
    
  
  
    
       return Boolean.parseBoolean(b);
    
  
  
    
    
    
      大家可以發(fā)現(xiàn),
    
    
      server
    
    
      端和
    
    
      client
    
    
      的中間傳遞數(shù)據都是通過
    
  
  
    
       org.apache.axiom.om.OMElement
    
    
      對象的,這個對象是一段
    
    
      xml
    
    
      的
    
    
      java 
    
    
      對象映射
    
    
      .
    
  
  
    
      疑惑
    
    
      :
    
  
  
    
      
      
    
    
      貌似是
    
    
      Axis2
    
    
      的
    
    
      bug,
    
    
      我在
    
    
       method buildUploadEnvelope
    
    
      最后是這樣寫的,
    
  
  
    
       data.addChild(mboxnum);
    
  
  
    
       data.addChild(gtType);
    
  
  
    
       data.addChild(fileType);
    
  
  
    
       data.addChild(fileContent);
    
  
  
    
       return data;
    
  
  
    
       data.addChild(fileContent);
    
    
      對應的是
    
  
  
    
    
    
      <x:upload
    
    
      xmlns:x=
    
    
      "http://example.org/mtom/data"
    
    
      >
    
  
  
    
    
    
      <x:mailboxnum>
    
    
      20060405
    
    
      </x:mailboxnum>
    
  
  
    
    
    
      <x:greetingType>
    
    
      1
    
    
      </x:greetingType>
    
  
  
    
    
    
      <x:fileType>
    
    
      wsdd
    
    
      </x:fileType>
    
  
  
    
    
    
      <x:fileContent>
    
  
  
    
      Dwvc2VydmljZT4NCjwvZGVwbG95bWVudD4NCg0K
    
  
  
    
       </x:fileContent>
    
  
  
    
    
    
      </x:upload>
    
    
      中紅色的部分。
    
  
  
    
    
    
      這樣沒有問題,
    
    
      service
    
    
      端接受到的
    
    
      message
    
    
      的數(shù)據跟
    
    
      client
    
    
      一致的。
    
  
  
    
    
    
      但是如果我這樣寫
    
    
      ,
    
    
      就是把要傳送的文件內容提前加入
    
  
  
    
    
    
      data.addChild(fileContent);
    
  
  
    
    
    
      data.addChild(mboxnum);
    
  
  
    
       data.addChild(gtType);
    
  
  
    
       data.addChild(fileType);
    
  
  
    
       return data;
    
  
  
    
       Client
    
    
      端的數(shù)據是這樣的:
    
  
  
    
    
    
      <x:upload
    
    
      xmlns:x=
    
    
      "http://example.org/mtom/data"
    
    
      >
    
  
  
    
    
    
      <x:fileContent>
    
  
  
    
      Dwvc2VydmljZT4NCjwvZGVwbG95bWVudD4NCg0K
    
  
  
    
       </x:fileContent>
    
  
  
    
    
    
      <x:mailboxnum>
    
    
      20060405
    
    
      </x:mailboxnum>
    
  
  
    
    
    
      <x:greetingType>
    
    
      1
    
    
      </x:greetingType>
    
  
  
    
    
    
      <x:fileType>
    
    
      wsdd
    
    
      </x:fileType>
    
  
  
    
    
    
      </x:upload>
    
  
  
    
    
    
      但是
    
    
      server
    
    
      接收到的數(shù)據成了
    
  
  
    
    
    
      <x:upload
    
    
      xmlns:x=
    
    
      "http://example.org/mtom/data"
    
    
      >
    
  
  
    
    
    
      <x:fileContent>
    
  
  
    
      Dwvc2VydmljZT4NCjwvZGVwbG95bWVudD4NCg0K
    
  
  
    
       </x:fileContent>
    
  
  
    
    
    
      </x:upload>
    
  
  
    
    
    
      后面的
    
    
      3
    
    
      條數(shù)據都沒了?。。。?!
    
  
  
    
      
        參考文獻:
      
    
  
  
    
       Axis2 user guide
    
  
  
    
       Axis2 sample(Axis2 binary distribution 
    
    
      自帶
    
    
      )
    
  
  
    
      
        友情提醒:
      
    
  
  
    
      
        
        
        
          如果大家要用
        
        
          Axis2
        
        
          開發(fā)
        
        
          webservice,
        
        
          強烈建議你先看
        
        
          IBM
        
        
          的
        
        
          
            
              developerWorks
              
中國 上的一個教程 -- 用 Apache Geronimo 和 Axis2 實現(xiàn)在線銀行,第 1 部分 : 服務:布設框架
  中國 上的一個教程 -- 用 Apache Geronimo 和 Axis2 實現(xiàn)在線銀行,第 1 部分 : 服務:布設框架
    
      URL
    
    
      為
    
    
      
        
          
            
              https://www6.software.ibm.com/developerworks/cn/education/opensource/os-ag-onbank1/index.html
            
          
        
      
    
    
      
        ,看這個教程需要注冊
      
    
    
      
        
          developerWorks
        
        
          中國
        
      
    
    
      的賬號,免費注冊的。
    
  
  
    
      這個教程的第
    
    
      2
    
    
      ,第
    
    
      3
    
    
      部分的
    
    
      url
    
    
      為
    
    
      :
    
  
  
  
  
    
      
        我從這個教程受益匪淺,相信你也一定會有收獲的。
      
    
  
  
    
      
        Contact:
      
    
  
  
    
      
        
          
            msn:
          
        
        samcyyl1229@hotmail.com
      
    
    
  
  
    
      
        QQ:363991184
      
    
  
  
更多文章、技術交流、商務合作、聯(lián)系博主
微信掃碼或搜索:z360901061
					微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
					
