`
wlh269
  • 浏览: 453093 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

axis2和JDK1.5开发(文件传输服务)详解一(图解)

阅读更多
1.准备资源:axis2涉及到的jar包和发布环境war包,地址如下:
http://apache.mirror.phpchina.com/ws/axis2/1_4_1/axis2-1.4.1-bin.zip
http://apache.mirror.phpchina.com/ws/axis2/1_4_1/axis2-1.4.1-war.zip
2.将下载后的war包解压成axis2.war,拷贝到tomcat的webapps下,启动tomcat,访问地址http://localhost:8081/axis2,如果看到那只似曾相识apache页面,那么恭喜你,成功了!



2.接着安装Eclipse插件,插件有两个,客户端代码插件axis2-eclipse-codegen-wizard.zip和服务端代码生成插件axis2-eclipse-service-archiver-wizard.zip,一般开发服务端的人只要有服务端代码生成插件即可,如果我们使用别人发布的WS,那么会用到客户端代码生成插件。总之都装上就是了,用的时候就方便了,将两个插件直接拷贝到myEclipse的 HOME\eclipse\plugins\下,然后再MyEclipse中选择File--new--others,打开new窗口。如果窗口中有Axis2 Wizards这个选项,那么插件安装成功!



3.编写服务端代码,新建一个web工程,记得将下载的axis2-1.4.1-bin.zip中的lib\*.jar都关联到工程的library中,下面是一段负责上传下载文件等功能的webservice服务端代码,仅供参考:

package com.hollyinfo.cuibao.webservice; 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.RandomAccessFile; 
import java.util.Iterator; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

import javax.activation.DataHandler; 
import javax.activation.FileDataSource; 

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 com.hollyinfo.cuibao.logger.HollyinfoLog; 
import com.hollyinfo.cuibao.logger.HollyinfoLogger; 
import com.hollyinfo.cuibao.util.Config; 

public class FileTransferServer { 
       public static final String UPLOAD_PATH = Config.getInstance().getProperty("UPLOAD_PATH"); 
       public static final String DOWNLOAD_PATH = Config.getInstance().getProperty("DOWNLOAD_PATH"); 
       private static final HollyinfoLog LOG = HollyinfoLogger.getLog(FileTransferServer.class); 
       public OMElement upload(OMElement element) throws Exception { 
       LOG.info("正在接收文件!"); 
       FileOutputStream ops=null; 
       InputStream ips=null; 
       OMElement ele=null; 
       try{ 
              OMElement _fileContent = null;//文件内容 
              OMElement _fileName = null;//文件名 
              OMElement _fileType = null;//文件类型 
              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 = UPLOAD_PATH ; 
              File dir = new File(storeDir); 
              if (!dir.exists()) { 
                     dir.mkdir(); 
              } 
              String filePath = storeDir + fileName + "." + fileType; 
              File uploadFile = new File(filePath); 
              // 解析上传数据,通过输出流保存文件 
              DataHandler actualDH = (DataHandler) binaryNode.getDataHandler(); 
               ips = actualDH.getInputStream(); 
               ops = new FileOutputStream(uploadFile); 
               //每次读取10KB,写到输出流 
              int bytesRead=0; 
              byte[] buf = new byte[10*1024]; //10KB 
              while((bytesRead = ips.read(buf)) != -1){ 
              ops.write(buf, 0, bytesRead); 
               } 
              OMFactory fac = OMAbstractFactory.getOMFactory(); 
              OMNamespace ns = fac.createOMNamespace("http://example.org/filedata", "fd"); 
              ele = fac.createOMElement("response", ns); 
              //检查文件是否完整传输 
              if(readLastLine(uploadFile,Config.getInstance().getProperty("FILE_CHARSET")).indexOf("EOF")!=-1){ 
                  ele.setText("true"); 
                  LOG.info("接收文件:"+filePath+" 成功!"); 
              }else{ 
              ele.setText("false"); 
              LOG.info("接收文件:"+filePath+" 没有完整接收!"); 
              } 
       } 
       catch(Exception e) { 
         e.printStackTrace(); 
         LOG.error("上传文件失败",e); 
       }finally{ 
          try { 
            if(ips!=null){ 
      ips.close(); 
          } 
            if(ops!=null){ 
            ops.close(); 
           } 
      } catch (IOException e) { 
      e.printStackTrace(); 
      } 
         
       } 
return ele; 
       } 
       
       
       /** 
        * 返回给客户端文件列表 
        * @param element 
        * @return 
        * @throws Exception 
        */ 
       public OMElement queryFileList(OMElement element) throws Exception { 
           OMElement flag = null;//请求标志 
           //OMElement firstElement=element.getFirstElement();   
           for (Iterator _iterator = element.getChildElements(); _iterator.hasNext();) { 
               OMElement _ele = (OMElement) _iterator.next(); 
               if (_ele.getLocalName().equalsIgnoreCase("flag")) { 
                      flag = _ele; 
                      break; 
               } 
            } 
            //如果客户端请求的是查询文件列表则返回文件列表 
            if("true".equals(flag.getText())){ 
            // String xmlInfo="111.txt"; 
                 OMFactory fac = OMAbstractFactory.getOMFactory(); 
                 //这个是命名空间,实际情况需要看对方给你的wsdl这个值是什么.这是服务器端随便你定义.客户端需要照这个样子传才行。 
                 //后面那个参数可为空也可以有值。这个东西也好理解调试的时候打印下OMElement 就知道这东西怎么用了。   
                 OMNamespace omNs = fac.createOMNamespace("http://example.org/filedata","fd"); 
                 OMElement filelist = fac.createOMElement("queryFileList", omNs); 
                 File dir=new File(DOWNLOAD_PATH); 
                 if(!dir.exists()){ 
                dir.mkdirs(); 
                 } 
                 LOG.info("查询待下载的文件列表: " + DOWNLOAD_PATH); 
                 String [] filenames=dir.list(); 
                 OMElement _file=null; 
                 for (int i = 0; i  0) {   
        pos--;   
        raf.seek(pos);   
        if (raf.readByte() == '\n') {   
          break;   
        }   
      }   
      if (pos == 0) {   
        raf.seek(0);   
      }   
      byte[] bytes = new byte[(int) (len - pos)];   
      raf.read(bytes);   
      if (charset == null) {   
        return new String(bytes);   
      } else {   
        return new String(bytes, charset);   
      }   
    }   
  } catch (FileNotFoundException e) {   
  } catch (IOException e) { 
e.printStackTrace(); 
} finally {   
    if (raf != null) {   
      try {   
        raf.close();   
      } catch (Exception e2) {   
      }   
    }   
  }   
  return null;   
} 
       
       
} 


4.编写services.xml文件,这个文件也可以自动生成,这里简单介绍一下这个文件的功能:这个文件用于描述我们将来服务的核心类,已经涉及到其中哪些方法,将来我们使用插件生成*.aar包的过程需要制定这个services.xml文件,最后我们需要部署aar包到tomcat中,这样服务也算发布完成!
  (1)、  中name属性的值是服务的名称;
  (2)、 值是服务类的全路径;
  (3)、 服务类的每个方法都写一个operation标签:
    
 <?xml version="1.0" encoding="UTF-8"?>
<service name="FileTransServer">
    <description>
       负责发催报信息的webservice
    </description>
    <parameter name="ServiceClass" locked="false">com.hollyinfo.cuibao.webservice.FileTransferServer</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>
    <operation name="queryFileList">
        <actionMapping>urn:queryFileList</actionMapping>
        <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
    </operation>
     <operation name="deleteFile">
        <actionMapping>urn:deleteFile</actionMapping>
        <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
    </operation>
</service>


  • 大小: 48.8 KB
  • 大小: 61.3 KB
分享到:
评论

相关推荐

    axis2_demo实战 复杂对象传输 文件传输

    这些资源可以帮助开发者了解并实践如何在Axis2中实现复杂的对象传输和文件传输。通过阅读和运行这些示例,开发者可以掌握如何在实际项目中使用Axis2来解决类似问题。 总的来说,这个实战教程将指导你如何利用Axis2...

    Axis2开发webservice总结.doc

    总结来说,Axis2是Apache软件基金会的一个开源项目,提供了一种高效且灵活的方式来开发和部署Web服务。开发者可以利用其强大的功能来构建复杂的分布式应用程序,实现跨平台的数据交换。理解Web服务的基本原理,熟练...

    WebService的开发模版(Axis2 1.5环境)

    在这个“WebService的开发模版(Axis2 1.5环境)”中,我们将探讨如何在Axis2 1.5框架下创建和使用Web服务。 Axis2是Apache软件基金会开发的一个Web服务引擎,它是Apache Axis1的升级版本,提供了更强大的功能和更...

    axis2 jdk1.4用jar

    标题“axis2 jdk1.4用jar”指的是在Java Development Kit (JDK) 1.4环境下使用Apache Axis2的特定版本,因为Axis2是一个基于Java的Web服务框架,它允许开发人员创建、部署和管理Web服务。在这个场景中,可能是指一个...

    axis2-xmlbeans-1.5.jar.zip

    标题中的"axis2-xmlbeans-1.5.jar.zip"是一...这个"axis2-xmlbeans-1.5.jar.zip"文件对于开发使用Axis2框架和XMLBeans处理XML的Java Web服务项目来说是一个重要的资源。它包含了运行和开发此类服务所需的库和许可信息。

    axis2-1.5.3-war.zip axis2-1.5.3-war.zip

    Axis2 是一个开源的Web服务平台,用于构建和部署Web服务。版本1.5.3是Axis2的一个特定发行版,它提供了许多增强的功能和修复了之前版本中的问题。这个压缩包“axis2-1.5.3-war.zip”包含了Axis2的Web应用版本,可以...

    axis2-1.5源码

    Axis2是Apache软件基金会开发的一个开放源码的Web服务引擎,它是基于SOAP(Simple Object Access Protocol)和WS-*(Web Services栈)规范的。这个"axis2-1.5源码"提供了对 Axis2 框架核心功能的深入了解,对于...

    axis2-1.5.6完整版

    Axis2 是一个强大的 Web Service 框架,它是由 Apache 软件基金会开发的,主要用于构建和部署 Web 服务。版本 1.5.6 是 Axis2 的一个稳定版本,提供了一系列增强的功能和修复了若干已知问题,使得在 SAP 中进行 Web ...

    axis2-1.5.jar

    官方版本,亲测可用

    axis1.4和axis2相关jar文件

    了解并熟练使用Axis1.4和Axis2的相关jar文件,对于开发和维护Web服务至关重要。这不仅可以帮助开发者快速构建服务,还能确保与现有系统和其他Web服务的互操作性。在实际项目中,应根据具体需求选择适合的版本,同时...

    axis2-kernel-1.5.jar

    官方版本,亲测可用

    Axis2范例,对象传输

    "Axis2范例,对象传输" 指的是使用Apache Axis2框架进行Web服务开发的一个示例项目,重点在于演示如何通过Axis2传输和处理对象,特别是涉及对象与XML之间的转换,以及在客户端和服务端之间的通信。 **描述详解:** ...

    WSDL2Java及axis2-1.5-bin.zip的包

    使用axis2-1.5开发包 从最简单的开始 ,-uri 指定wsdl文件&gt; WSDL2Java -uri currencyConvert.wsdl-d 使用不同的data binding方法&gt; WSDL2Java -uri currencyConvert.wsdl -d xmlbeans-a 生成异步的方法&gt; WSDL2Java -...

    AXIS2远程调用WebService示例(Eclipse+AXIS2)工具和所用包.rar

    3. AXIS2:Apache AXIS2是Java平台上用于构建和使用Web服务的一个高性能、轻量级的框架。 在开始之前,请确保你的开发环境已安装了JRE和JDK1.8。JRE是Java运行时环境,而JDK则包含编译器和其他开发工具,对于开发...

    axis2-1.5.3-bin.zip axis2-1.5.3-bin.zip axis2-1.5.3-bin.zip

    Apache Axis2是Apache软件基金会的Web服务引擎,它为开发和部署SOAP 1.1/1.2和RESTful Web服务提供了一个高效、灵活的平台。Axis2的主要特点包括模块化架构、消息传递机制和多种绑定支持,如HTTP、JMS、SMTP等。 2...

    axis2的bin和war文件

    Apache Axis2是著名的Java Web服务框架,用于创建和部署SOAP(简单对象访问协议)和RESTful ...综上所述,Axis2的bin和war文件为Java开发者提供了两种不同方式来部署和管理Web服务,满足了不同场景下的开发和运维需求。

    axis 1.x与axis2.x开发

    Axis是Apache组织开发的一款开源Web服务框架,主要用于构建和部署SOAP(Simple Object Access Protocol)服务。Axis分为两个主要版本:Axis1.x和Axis2.x,它们都是Java平台上的Web服务实现,但在设计和功能上有所...

Global site tag (gtag.js) - Google Analytics