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

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

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

    Terry.Li-彬

    虛其心,可解天下之問;專其心,可治天下之學;靜其心,可悟天下之理;恒其心,可成天下之業。

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      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); //必須加此句,否則會出現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.結果
    察看soap消息,我們可以發現
    <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發送附件應用了builder模式。要向一個webserive 發送請求,首先就要構建一個請求的Envelope,Axis2構建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;
    }
    這一段其實是構建的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是要傳送的文件的內容代表,至于什么編碼,我沒有深究。注意這一句:
    OMElement data = fac.createOMElement("upload", omNs);
    這里的“upload”參數對應的是webservice的一個操作的名稱,這個操作的名稱是跟webserviceserver端實現類的方法名和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;
       }
    這里構建的Options對象,顧名思義就是調用webservice的相應的選項:比如這里就指定了Soap協議為1.1 ,指定了所請求的service EPR(就是地址),聲明在client應用MTOM指定傳輸協議為HTTP
    構建好要傳送的dataoptions,所執行的代碼為:
    ServiceClient sender = new ServiceClient();
    //設定選項
    sender.setOptions(options);
    //打印要傳送的數據,為一段xml
    System.out.println(data);
    //傳送數據,得到返回值
    OMElement ome = sender.sendReceive(data);
    //打印返回值,為一段xml
    System.out.println(ome);
    //析取返回值中的數據
    String b = ome.getText();
    //返回
    return Boolean.parseBoolean(b);
    可以發現,server端和client的中間傳遞數據都是通過      org.apache.axiom.om.OMElement對象的,這個對象是一段xmljava 對象映射。
    posted on 2008-07-10 16:54 禮物 閱讀(833) 評論(0)  編輯  收藏 所屬分類: web service
    主站蜘蛛池模板: 成人电影在线免费观看| 亚洲中文字幕久久精品无码A| 亚洲成AV人在线观看网址| 免费日韩在线视频| 亚洲精品NV久久久久久久久久| 亚洲色欲久久久综合网东京热| 亚洲avav天堂av在线不卡 | 亚洲日韩精品射精日| 一个人晚上在线观看的免费视频| 黄色视屏在线免费播放| 91精品国产免费久久国语蜜臀 | 国产亚洲精品资源在线26u| 亚洲码在线中文在线观看| 亚洲女子高潮不断爆白浆| EEUSS影院WWW在线观看免费| 亚洲一区二区免费视频| 日本特黄特色aa大片免费| 亚洲另类激情专区小说图片| 成人a毛片视频免费看| 日本h在线精品免费观看| 亚洲沟沟美女亚洲沟沟| 一个人看的www在线观看免费| 中文字幕精品亚洲无线码二区| 亚洲一区二区久久| 一级毛片大全免费播放| 久久久久亚洲精品中文字幕| 亚洲一久久久久久久久| 四虎免费永久在线播放| 久久久久久噜噜精品免费直播| 亚洲色精品aⅴ一区区三区| 最近2022中文字幕免费视频| 亚洲色中文字幕在线播放| 亚洲伊人久久综合中文成人网 | 亚洲中文字幕无码av永久| 免费观看日本污污ww网站一区| 中国精品一级毛片免费播放| 四虎成人精品在永久免费| 岛国岛国免费V片在线观看| 337p日本欧洲亚洲大胆精品555588 | 亚洲精品二区国产综合野狼| 最近2018中文字幕免费视频|