微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

利用axis创建webservice实现文件传输

WebService处理传递普通的信息,还可以传输文件,下面介绍WebService是怎么完成文件传输的。

1、 首先编写服务器端上传文件的WebService方法


package com.hoo.service;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import javax.activation.DataHandler;

/**
 * axis WebService完成文件上传服务器端
 */
public class UploadFileService {

    public String upload(DataHandler handler,String fileName) {
        if (fileName != null && !"".equals(fileName)) {
            File file = new File(fileName);
            if (handler != null) {
                InputStream is = null;
                FileOutputStream fos = null;
                try {
                    is = handler.getInputStream();
                    fos = new FileOutputStream(file);
                    byte[] buff = new byte[1024 * 8];
                    int len = 0;
                    while ((len = is.read(buff)) > 0) {
                        fos.write(buff,len);
                    }
                } catch(FileNotFoundException e) {
                    return "fileNotFound";
                } catch (Exception e) {
                    return "upload File failure";
                } finally {
                    try {
                        if (fos != null) {
                            fos.flush();
                            fos.close();
                        }
                        if (is != null) {
                            is.close();
                        }
                    } catch (Exception e) {
                        e.printstacktrace();
                    }
                }
                return "file absolute path:" + file.getAbsolutePath();
            } else {
                return "handler is null";
            }
        } else {
            return "fileName is null";
        }
    }
}

上传方法和我们以前在Web中上传唯一不同的就是参数一DataHandler,可以将这类看成文件传输器,他可以把文件序列化。
然后通过DataHandler可以得到一个输入流InputStream,通过这个流可以读到文件内容。其他的操作和普通上传类似。

2、 定制wsdd发布文件上传的WebService服务,在WEB-INF/目录下创建deploy.wsdd文件文件内容如下:

<?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">
    <service name="UploadFile" provider="java:RPC">
        <parameter name="className" value="com.hoo.service.UploadFileService" />
        <parameter name="allowedMethods" value="*" />
        <parameter name="scope" value="Session" />
        <!-- 和服务器端上传文件方法签名对应,参数也对应 -->
        <operation name="upload" qname="operNS:upload" xmlns:operNS="upload" returnType="rns:string"
            xmlns:rns="http://www.w3.org/2001/XMLSchema">
            <parameter name="handler" type="ns:DataHandler" xmlns:ns="http://www.w3.org/2001/XMLSchema"/>
            <parameter name="fileName" type="ns:string" xmlns:ns="http://www.w3.org/2001/XMLSchema"/>
        </operation>
        <typeMapping qname="hns:DataHandler" xmlns:hns="ns:FileUploadHandler"  
            languageSpecificType="java:javax.activation.DataHandler"
    serializer="org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory" deserializer="org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
    </service>
</deployment>

 上面才xml节点元素在前面都见过了,说明下operation中的参数,注意要指定参数类型,特别是DataHandler的类型,然后就是typeMapping的serializer、deserializer的序列化和反序列化工厂类的配置。这里的deploy.wsdd是我们刚才定制的wsdd文件,java当然是jvm的命令,-Djava.ext.dirs=lib设置当前命令的依赖包,AdminClient是axis提供的工具类,这个类本来是可以在官方的工程中admin可以直接运行的(这里不可以,下载下来的少了AdminServlet,有兴趣的可以研究下,就是前面说的官方的示例);

3、 用dos命令发布当前WebService C:\apache-tomcat-5.5.26\webapps\axis\WEB-INF>java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -lhttp://localhost:8080/axis/services/AdminService deployUpload.wsdd 发布完成后,可以通过这个地址查看uploadFile这个service了 http://localhost:8080/axis/servlet/AxisServlet 4、 编写客户端代码   package com.hoo.client; import java.rmi.remoteexception; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode; import javax.xml.rpc.ServiceException; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType; import org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory; import org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory; /**  * 上传文件WebService客户端  */ public class UploadFileClient {     public static void main(String[] args) throws ServiceException,remoteexception {         String url = "http://localhost:8080/AxisWebService/services/UploadFile";         String fileName = "readMe.txt";         String path = System.getProperty("user.dir") + "\\Webroot\\" + fileName;         System.out.println(path);                  //这样就相当于构造了一个文件路径的File了         DataHandler handler = new DataHandler(new FileDataSource(path));                  Service service = new Service();         Call call = (Call) service.createCall();         call.setTargetEndpointAddress(url);                  /**          * 注册异常类信息和序列化类 ns:FileUploadHandler 和 wsdd 配置文件中的typeMapping中的xmlns:hns="ns:FileUploadHandler" 的对应 DataHandler          * 和 wsdd 配置文件中的typeMapping中的qname="hns:DataHandler"的DataHandler对应          */         QName qn = new QName("ns:FileUploadHandler","DataHandler");         call.registerTypeMapping(DataHandler.class,qn,                JAFDataHandlerSerializerFactory.class,                JAFDataHandlerDeserializerFactory.class);         call.setoperationName(new QName(url,"upload"));                  //设置方法形参,注意的是参数1的type的DataHandler类型的,和上面的qn的类型是一样的         call.addParameter("handler",ParameterMode.IN);         call.addParameter("fileName",XMLType.XSD_STRING,ParameterMode.IN);         //设置返回值类型,下面2种方法都可以         call.setReturnClass(String.class);         //call.setReturnType(XMLType.XSD_STRING);                  String result = (String) call.invoke(new Object[] { handler,"remote_server_readMe.txt" });         System.out.println(result);     } }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐