<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
    版權聲明:原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。http://zhangjunhd.51cto.com/113473/26960
    本文介紹如何使用Axis2傳遞附件。
    author: ZJ 07-5-7
     
    1.工作環境
    IDE: Eclipse 3.1.2
    jdk: jdk1.5.0_04
    Tomcat: apache-tomcat-5.0.28
    AXIS2:1.0(war版本和bin版本)
     
    2.實現
       Eclipse新建一個動態web工程,在WEB-INF"lib下加入axis2所需的jar包。
    本例的是一個系統的用戶上傳下載圖片格式文件的例子,每次上傳出攜帶附件外,還包括文件名, 文件類型。此webservice實現的2個功能就是upload, download.
      AXIS2webservice發布的時候是打包成xxx.aar發布的,xxx.aar展開后的目錄結構為
     --
        --META-INF
           services.xml
        --包含server端實現的class( 目錄跟package是一樣的結構)
     
    3.服務器端FileTransferServer.java
    package sample;
     
    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.File;
    import java.io.FileOutputStream;
    import java.io.InputStream;
     
    import java.util.Iterator;
     
    import javax.activation.DataHandler;
    import javax.activation.FileDataSource;
     
    public class FileTransferServer {
           public static final String TMP_PATH = "D:/temp";
     
           public OMElement upload(OMElement element) throws Exception {
                  OMElement _fileContent = null;//文件內容
                  OMElement _fileName = null;//文件名
                  OMElement _fileType = null;//文件類型
                  System.out.println("The element for upload: " + element);
                  for (Iterator _iterator = element.getChildElements(); _iterator
                                .hasNext();) {
                         OMElement _ele = (OMElement) _iterator.next();
                         if (_ele.getLocalName().equalsIgnoreCase("fileContent")) {
                                _fileContent = _ele;
                         }
                         if (_ele.getLocalName().equalsIgnoreCase("fileName")) {
                                _fileName = _ele;
                         }
                         if (_ele.getLocalName().equalsIgnoreCase("fileType")) {
                                _fileType = _ele;
                         }
                  }
     
                  if (_fileContent == null || _fileType == null) {
                         throw new AxisFault("Either Image or FileName is null");
                  }
     
                  OMText binaryNode = (OMText) _fileContent.getFirstOMChild();
                  String fileName = _fileName.getText();
                  String fileType = _fileType.getText();
                  String storeDir = TMP_PATH + "/" + "tempTest";
                  File dir = new File(storeDir);
                  if (!dir.exists()) {
                         dir.mkdir();
                  }
                  String filePath = storeDir + "/" + fileName + "." + fileType;
                  File uploadFile = new File(filePath);
                  if (uploadFile.exists()) {
                         filePath = storeDir + "/" + fileName + "(1)" + "." + fileType;
                         uploadFile = new File(filePath);
                  }
     
                  // Extracting the data and saving
                  DataHandler actualDH;
                  actualDH = (DataHandler) binaryNode.getDataHandler();
     
                  FileOutputStream imageOutStream = new FileOutputStream(uploadFile);
                  InputStream is = actualDH.getInputStream();
                  imageOutStream.write(IOUtils.getStreamAsByteArray(is));
                  // setting response
                  OMFactory fac = OMAbstractFactory.getOMFactory();
                  OMNamespace ns = fac.createOMNamespace("http://example.org/filedata",
                                "fd");
                  OMElement ele = fac.createOMElement("response", ns);
                  ele.setText("true");
                  return ele;
           }
     
           public OMElement download(OMElement element) throws Exception {
                  System.out.println("The element for download: " + element);
                  OMElement _userName = null;
                  OMElement _fileName = null;
                  OMElement _fileType = null;
                  for (Iterator _iterator = element.getChildElements(); _iterator
                                .hasNext();) {
                         OMElement _ele = (OMElement) _iterator.next();
                         if (_ele.getLocalName().equalsIgnoreCase("userName")) {
                                _userName = _ele;
                         }
                         if (_ele.getLocalName().equalsIgnoreCase("fileName")) {
                                _fileName = _ele;
                         }
                         if (_ele.getLocalName().equalsIgnoreCase("fileType")) {
                                _fileType = _ele;
                         }
                  }
                  String userName = _userName.getText();
                  String fileName = _fileName.getText();
                  String fileType = _fileType.getText();
                  String filePath = TMP_PATH + "/" + userName + "/" + fileName + "."
                                + fileType;
                  System.out.println("The filePath for download: " + filePath);
                  FileDataSource dataSource = new FileDataSource(filePath);
                  DataHandler expectedDH = new DataHandler(dataSource);
                  OMFactory fac = OMAbstractFactory.getOMFactory();
                  OMNamespace ns = fac.createOMNamespace("http://example.org/filedata",
                                "fd");
                  OMText textData = fac.createOMText(expectedDH, true);
                  OMElement ele = fac.createOMElement("response", ns);
                  ele.addChild(textData);
                  return ele;
           }
    }
     
    4.services.xml
       
            This is a sample Web Service with two operations,echo and ping.
       
        sample.FileTransferServer
       
            urn:upload
           
       
         
            urn:download
           
       
     
    將這兩個文件打包并部署到Tomcat上(略)。
     
    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;
    posted on 2008-07-10 16:54 禮物 閱讀(1442) 評論(0)  編輯  收藏 所屬分類: web service
    主站蜘蛛池模板: 亚洲精品在线电影| 亚洲中文无码永久免费| 亚洲免费网站观看视频| 久久久久亚洲AV成人无码网站| 日本特黄特色aa大片免费| 5g影院5g天天爽永久免费影院| 七次郎成人免费线路视频| 亚洲AV无码成人精品区日韩| 亚洲国产精品综合久久久| 亚洲国产精品无码专区影院| www国产亚洲精品久久久| 成年性午夜免费视频网站不卡| 99re免费99re在线视频手机版| 中文在线免费看视频| www成人免费视频| 精品国产日韩亚洲一区91| 亚洲综合av一区二区三区| 亚洲免费视频网址| 亚洲视频在线不卡| 亚洲国产成人久久综合一| 国产亚洲一区二区手机在线观看| 亚洲第一区在线观看| 免费看国产精品麻豆| 永久免费毛片手机版在线看| 西西大胆无码视频免费| 精品久久久久成人码免费动漫 | 青青青免费国产在线视频小草| 四虎成人精品永久免费AV| 最近中文字幕免费大全| www永久免费视频| aa毛片免费全部播放完整| 亚洲视频在线免费| a级毛片免费观看网站| a毛片成人免费全部播放| 一本久久免费视频| 一级大黄美女免费播放| 久久精品无码免费不卡| 97无码人妻福利免费公开在线视频 | 一级毛片大全免费播放下载| 一本久久免费视频| 中文字幕免费视频精品一|