jjklm

axis 开发 web services

一 、Axis的运行需要如下组件包

axis.jar、jaxrpc.jar、saaj.jar、commons-logging.jar、commons-discovery.jar、wsdl4j.jar、符合JAXP-1.1的XML处理器。
二、在WEB-INF下建立server-config.xml文件
代码示例如下:
<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
 <globalConfiguration>
  <parameter name="adminPassword" value="admin"/>
  <parameter name="attachments.Directory" value="D:\eclipse3.1\workspace\ForWebservice\WEB-INF\attachments"/>
  <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/>
  <parameter name="sendXsiTypes" value="true"/>
  <parameter name="sendMultiRefs" value="true"/>
  <parameter name="sendXMLDeclaration" value="true"/>
  <parameter name="axis.sendMinimizedElements" value="true"/>
  <requestFlow>
   <handler type="java:org.apache.axis.handlers.JWSHandler">
    <parameter name="scope" value="session"/>
   </handler>
   <handler type="java:org.apache.axis.handlers.JWSHandler">
    <parameter name="scope" value="request"/>
    <parameter name="extension" value=".jwr"/>
   </handler>
  </requestFlow>
 </globalConfiguration>
 <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/>
 <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>
 <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
 <service name="Version" provider="java:RPC">
  <parameter name="allowedMethods" value="getVersion"/>
  <parameter name="className" value="org.apache.axis.Version"/>
 </service>
 <!--这里就是我们提供的服务名称-->
  <service name="FileProcessEnter" provider="java:RPC">
  <parameter name="allowedMethods" value="*"/>
<!--我们程序中对应的JAVA类的名称-->
  <parameter name="className" value="com.nci.fm.file.FileProcessEnter"/>
 </service>
 <service name="AdminService" provider="java:MSG">
  <parameter name="allowedMethods" value="AdminService"/>
  <parameter name="enableRemoteAdmin" value="false"/>
  <parameter name="className" value="org.apache.axis.utils.Admin"/>
  <namespace>http://xml.apache.org/axis/wsdd/</namespace>
 </service>
 <transport name="local">
  <responseFlow>
   <handler type="LocalResponder"/>
  </responseFlow>
 </transport>
 <transport name="http">
  <requestFlow>
   <handler type="URLMapper"/>
   <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
  </requestFlow>
 </transport>
</deployment>
三、在WEB-INF中建立web.xml文件
代码示例如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
 <listener>
        <listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class>
    </listener>

    <filter>
        <filter-name>Set Character Encoding</filter-name>
        <filter-class>com.nci.jfw.system.filter.SetCharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>GB2312</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>Set Character Encoding</filter-name>
        <servlet-name>action</servlet-name>
    </filter-mapping>
      <servlet>
    <servlet-name>AxisServlet</servlet-name>
    <display-name>Apache-Axis Servlet</display-name>
    <servlet-class>
        org.apache.axis.transport.http.AxisServlet
    </servlet-class>
  </servlet>

  <servlet>
    <servlet-name>AdminServlet</servlet-name>
    <display-name>Axis Admin Servlet</display-name>
    <servlet-class>
        org.apache.axis.transport.http.AdminServlet
    </servlet-class>
    <load-on-startup>100</load-on-startup>
  </servlet>

  <servlet>
    <servlet-name>SOAPMonitorService</servlet-name>
    <display-name>SOAPMonitorService</display-name>
    <servlet-class>
        org.apache.axis.monitor.SOAPMonitorService
    </servlet-class>
    <init-param>
      <param-name>SOAPMonitorPort</param-name>
      <param-value>5001</param-value>
    </init-param>
    <load-on-startup>100</load-on-startup>
  </servlet>
  

 

 

 <servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>/servlet/AxisServlet</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>*.jws</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>SOAPMonitorService</servlet-name>
    <url-pattern>/SOAPMonitor</url-pattern>
  </servlet-mapping> 
    <mime-mapping>
        <!-- WML Source -->
        <extension>wml</extension>
        <mime-type>text/vnd.wap.wml</mime-type>
    </mime-mapping>
    <mime-mapping>
        <!-- Compiled WML -->
        <extension>wmlc</extension>
        <mime-type>application/vnd.wap.wmlc</mime-type>
    </mime-mapping>
    <mime-mapping>
        <!-- WML Script Source -->
        <extension>wmls</extension>
        <mime-type>text/vnd.wap.wmlscript</mime-type>
    </mime-mapping>
    <mime-mapping>
        <!-- Compiled WML Script -->
        <extension>wmlscriptc</extension>
        <mime-type>application/vnd.wap.wmlscriptc</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>wrl</extension>
        <mime-type>x-world/x-vrml</mime-type>
    </mime-mapping>
    <mime-mapping>
  <extension>wsdl</extension>
  <mime-type>text/xml</mime-type>
    </mime-mapping>
    </web-app>
四、测试类
代码示例如下:
package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.namespace.QName;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class TestCallWS {
  public static byte[] getBytesFromFile(File file) throws IOException {
         InputStream is = new FileInputStream(file);
    
         // 获得file的size
         long length = file.length();
    
         // You cannot create an array using a long type.
         // It needs to be an int type.
         // Before converting to an int type, check
         // to ensure that file is not larger than Integer.MAX_VALUE.
         if (length > Integer.MAX_VALUE) {
             // File is too large
         }
    
         // Create the byte array to hold the data
         byte[] bytes = new byte[(int)length];
    
         // Read in the bytes
         int offset = 0;
         int numRead = 0;
         while (offset < bytes.length
                && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
             offset += numRead;
         }
    
         // Ensure all the bytes have been read in
         if (offset < bytes.length) {
             throw new IOException("Could not completely read file "+file.getName());
         }
    
         // Close the input stream and return bytes
         is.close();
         return bytes;
     }
  
  public static void main(String[] args){
   try{
    java.io.File f=new java.io.File("d:\\Blue.jpg");
    System.out.println("File'S LENGH="+f.length());
    System.out.println("File is OK!");
    byte[] bytes = new byte[(int)f.length()];
    
    bytes = getBytesFromFile(f);
    System.out.println("bytes's length="+bytes.length);
    System.out.println("bytes is OK!");
    //
    String endpoint = "http://192.168.131.156:8080/fms/services/FileProcessEnter";
    System.out.println("endpoint is OK!");
    //
    Service  service = new Service();
    Call  call  = (Call) service.createCall();
    System.out.println("Call is OK!");
    //
    call.setTargetEndpointAddress(new java.net.URL(endpoint) );
    call.setOperationName(new QName("http://soapinterop.org/", "InsertFile"));
    System.out.println("setOperationName is OK!");
    //call.addParameter("bytes",org.apache.axis.encoding.XMLType.XSD_BYTE,javax.xml.rpc.ParameterMode.IN);
    
    //call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);

    //
    String ret = (String) call.invoke( new Object[] {"EPMS","epms",bytes,"jpg" } );
    System.out.println(ret);
    
   }catch(Exception e){
    e.printStackTrace();
   }
  }
 }


 

posted on 2007-01-23 11:13 天涯孤客 阅读(110) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: