<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Terry.Li-彬

    虛其心,可解天下之問;專其心,可治天下之學(xué);靜其心,可悟天下之理;恒其心,可成天下之業(yè)。

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      143 隨筆 :: 344 文章 :: 130 評論 :: 0 Trackbacks
    5.測試
    FileTransferClient.java
    package sample;
     
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.InputStream;
     
    import javax.activation.DataHandler;
    import javax.activation.FileDataSource;
     
    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.axiom.soap.SOAP11Constants;
    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;
     
    public class FileTransferClient {
       private static EndpointReference targetEPR =
     new EndpointReference("http://127.0.0.1:8080/axis2/services/FileOperation");
      
       public static boolean upload(String fileName, File file, String fileType) {
         try {
          OMElement data = buildUploadEnvelope(fileName, file, fileType);
          Options options = buildOptions();
          ServiceClient sender = new ServiceClient();
          sender.setOptions(options);
          System.out.println("The data in method upload: "+data);
          OMElement ome = sender.sendReceive(data);
          System.out.println("Convert the data to element in method upload: "+ome);
          String b = ome.getText();
          return Boolean.parseBoolean(b);
         }
         catch(Exception e) {
           e.printStackTrace();
         }
         return false;
       }
      
       public static boolean download(String userName, String fileName, String fileType) {
         try {
           OMElement data = buildDownloadEnvelope(userName, fileName, fileType);
           Options options = buildOptions();
           ServiceClient sender = new ServiceClient();
           sender.setOptions(options);
           System.out.println("The data in method download: "+data);
           OMElement ome = sender.sendReceive(data);
           System.out.println("Convert the data to element in method download: "+ome);
           OMText binaryNode = (OMText) ome.getFirstOMChild();
           binaryNode.setOptimize(true); //必須加此句,否則會出現(xiàn)ContentID is null的異常!
           DataHandler actualDH = (DataHandler) binaryNode.getDataHandler();
           FileOutputStream imageOutStream = new FileOutputStream("D:/userTemp/xx.gif");
           InputStream is = actualDH.getInputStream();
           imageOutStream.write(IOUtils.getStreamAsByteArray(is));
           return true;
          }
          catch(Exception e) {
            e.printStackTrace();
          }
         return false;
       }
      
       private static OMElement buildUploadEnvelope(String fileName, File file, String fileType) {
         DataHandler expectedDH;
         OMFactory fac = OMAbstractFactory.getOMFactory();
         OMNamespace omNs = fac.createOMNamespace("http://example.org/filedata", "fd");
         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 _fileName = fac.createOMElement("fileName", omNs);
         _fileName.setText(fileName);
         OMElement _fileType = fac.createOMElement("fileType", omNs);
         _fileType.setText(fileType);
         data.addChild(_fileName);
         data.addChild(_fileType);
         data.addChild(fileContent);
         return data;
       }
      
       private static OMElement buildDownloadEnvelope(String userName, String fileName, String fileType) {
         OMFactory fac = OMAbstractFactory.getOMFactory();
         OMNamespace omNs = fac.createOMNamespace("http://example.org/filedata", "fd");
         OMElement data = fac.createOMElement("download", omNs);
         OMElement _userName = fac.createOMElement("userName", omNs);
         _userName.setText(userName);
         OMElement _fileName = fac.createOMElement("fileName", omNs);
         _fileName.setText(fileName);
         OMElement _fileType=fac.createOMElement("fileType", omNs);
         _fileType.setText(fileType);
         data.addChild(_userName);
         data.addChild(_fileName);
         data.addChild(_fileType);
         return data;
       }
       private static Options buildOptions() throws AxisFault {
         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 = "D:/userTemp/ya.gif";
         String fn = "testUser";
         String ft="gif";
         boolean rtv = upload(fn,new File(file),ft);
         System.out.println("is upload success: "+rtv);
         String un="zj";
         String downfn="1";
         if(download(un,downfn,ft)){
                System.out.println("download success.");
         }
         else System.out.println("download fail.");
         System.out.println("Client main end.");
       }
    }
     
    6.結(jié)果
    察看soap消息,我們可以發(fā)現(xiàn)
    <fd:upload xmlns:fd="http://example.org/filedata">
    <fd:fileName>testUser</fd:fileName>
    <fd:fileType>gif</fd:fileType>
    <fd:fileContent>RHQMLJJ4/AMZkEBAEAOw(省略部分2進制代碼)</fd:fileContent>
    </fd:upload>
     
    Convert the data to element in method upload:
    <fd:response xmlns:fd=http://example.org/filedata xmlns:tns="http://ws.apache.org/axis2">true</fd:response>
     
    The data in method download:
    <fd:download xmlns:fd="http://example.org/filedata">
    <fd:userName>zj</fd:userName>
    <fd:fileName>1</fd:fileName>
    <fd:fileType>gif</fd:fileType>
    </fd:download>
     
    Convert the data to element in method download:
    <fd:response xmlns:fd="http://example.org/filedata" xmlns:tns="http://ws.apache.org/axis2">
    eIqGRwjkQAAAOw==(省略部分2進制代碼)
    </fd:response>
     
    7.代碼分析
      利用Axis2Mtom發(fā)送附件應(yīng)用了builder模式。要向一個webserive 發(fā)送請求,首先就要構(gòu)建一個請求的Envelope,Axis2構(gòu)建Envelope的時候是利用的Axis2AXIOM api(就是axis2java objectxml的映射處理機制),其編程模式和DOM差不多的.看這一段:
    private static OMElement buildUploadEnvelope(String mailboxnum, short greetingType, File file, String FileType) {
    DataHandler expectedDH;
         OMFactory fac = OMAbstractFactory.getOMFactory();
    ...
    return data;
    }
    這一段其實是構(gòu)建的data對像是這樣一段xmljava object代表:
    <fd:upload xmlns:fd="http://example.org/filedata">
    <fd:fileName>testUser</fd:fileName>
    <fd:fileType>gif</fd:fileType>
    <fd:fileContent>RHQMLJJ4/AMZkEBAEAOw(省略部分2進制代碼)</fd:fileContent>
    </fd:upload>
    其中的Dwvc2VydmljZT4NCjwvZGVwbG95bWVudD4NCg0K是要傳送的文件的內(nèi)容代表,至于什么編碼,我沒有深究。注意這一句:
    OMElement data = fac.createOMElement("upload", omNs);
    這里的“upload”參數(shù)對應(yīng)的是webservice的一個操作的名稱,這個操作的名稱是跟webserviceserver端實現(xiàn)類的方法名和services.xml的所定義的
    <operationname="upload">
       <actionMapping>urn:upload</actionMapping>
       <messageReceiverclass="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
    </operation>
    要一致的。
    再看看這一段,
     private static Options buildOptions() {
         Options options = new Options();
         ...
         return options;
       }
    這里構(gòu)建的Options對象,顧名思義就是調(diào)用webservice的相應(yīng)的選項:比如這里就指定了Soap協(xié)議為1.1 ,指定了所請求的service EPR(就是地址),聲明在client應(yīng)用MTOM指定傳輸協(xié)議為HTTP
    構(gòu)建好要傳送的dataoptions,所執(zhí)行的代碼為:
    ServiceClient sender = new ServiceClient();
    //設(shè)定選項
    sender.setOptions(options);
    //打印要傳送的數(shù)據(jù),為一段xml
    System.out.println(data);
    //傳送數(shù)據(jù),得到返回值
    OMElement ome = sender.sendReceive(data);
    //打印返回值,為一段xml
    System.out.println(ome);
    //析取返回值中的數(shù)據(jù)
    String b = ome.getText();
    //返回
    return Boolean.parseBoolean(b);
    可以發(fā)現(xiàn),server端和client的中間傳遞數(shù)據(jù)都是通過      org.apache.axiom.om.OMElement對象的,這個對象是一段xmljava 對象映射。
    posted on 2008-07-10 16:54 禮物 閱讀(836) 評論(0)  編輯  收藏 所屬分類: web service
    主站蜘蛛池模板: 最近中文字幕大全免费视频| 一级成人a做片免费| 99久热只有精品视频免费观看17| 亚洲一区精品无码| 老司机精品免费视频| 亚洲日本中文字幕一区二区三区 | 亚洲国产精品久久久久网站| 中文无码成人免费视频在线观看 | 污污污视频在线免费观看| 美女黄网站人色视频免费国产| 亚洲乱码一二三四区国产| 亚洲精品免费网站| 亚洲中文无码mv| 内射无码专区久久亚洲| 精品97国产免费人成视频| 亚洲成AV人片在线观看WWW| 亚洲码欧美码一区二区三区| 免费看大黄高清网站视频在线| 国产成人亚洲毛片| 亚洲精品无码久久久久| 最近中文字幕大全中文字幕免费| 亚洲AV无码久久久久网站蜜桃| 成全视频在线观看免费高清动漫视频下载 | 久久免费精品视频| 日韩精品成人亚洲专区| 国产精品永久免费视频| 亚洲国产一区二区三区青草影视| 亚洲一区二区三区免费在线观看 | 亚洲中文字幕丝袜制服一区| 免费国产污网站在线观看| 亚洲国产精品日韩在线观看| 日韩激情淫片免费看| 99视频免费在线观看| 亚洲成av人片不卡无码| 亚洲AV无码乱码精品国产| 国产99视频精品免费专区| 亚洲日韩国产一区二区三区在线| 亚洲天堂中文字幕在线| 日韩在线播放全免费| 特色特黄a毛片高清免费观看| 亚洲综合日韩中文字幕v在线|