webservice文件上传下载

使用DataHandler实现webservice的文件上传下载

服务端代码:

package com.hello.weChat.controller;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by winkey on 15-2-10.
 */
public class FileController {
    /*
     * 文件上传服务
     */
    public String upload(DataHandler handler, String fileName) {
        String fullPath = "F:\\test\\testupload\\" + fileName;
        File file = new File(fullPath);
        int flag = 0;
        if (handler == null)
            return "failure";
        if (fileName == null)
            return "failure";
        InputStream input = null;
        FileOutputStream fos = null;
        try {
            input = handler.getInputStream();
            fos = new FileOutputStream(file);
            byte[] buffer = new byte[4096];
            while ((flag = input.read(buffer)) != -1) {
                fos.write(buffer, 0, flag);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "failure";
        } finally {
            try {
                if (input != null) {
                    input.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "Success";
    }

    /*
     * 文件下载服务
     */
    public DataHandler downloadFile() {
        String filepath = "F:\\test\\head.jpg";
        DataHandler dataHandler = new DataHandler(new FileDataSource(filepath));
        System.out.println("-------------------------下载");
        return dataHandler;
    }


}

服务端wsdd文件配置

<service name="FileController" provider="java:RPC">  
         <parameter name="className" value="com.hello.weChat.controller.FileController"/>  
         <parameter name="allowedMethods" value="downloadFile,upload"/> 
        <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:FileHandler"
                     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>

下载的时候只需配置typeMapping,上传的时候需要同时配置operation和typeMaping

客户端:

下载测试类:

package com.hello.weChat.controller;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory;
import org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory;

import javax.activation.DataHandler;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import java.io.*;
import java.rmi.RemoteException;

/**
 * Created by winkey on 15-2-10.
 */
public class downloadTest {
    public static void main(String[] args) {   // TODO Auto-generated method stub
        String url = "http://192.168.129.245:8080/btm-szny/services/FileController";
        Service service = new Service();
        Call call;
        DataHandler handler;
        InputStream input = null;
        FileOutputStream fos = null;
        String filepath = "f:/test/";
        String filename = filepath + "tests.jpg";
        File file = new File(filename);
        try {
            call = (Call) service.createCall();
            /**
             *
             * 注册异常类信息和序列化类
             * ns:FileHandler 和 wsdd 配置文件中的typeMapping中的xmlns:hns="ns:FileHandler" 的对应
             * DataHandler和 wsdd 配置文件中的typeMapping中的qname="hns:DataHandler"的DataHandler对应
             *
             */
            QName qn = new QName("ns:FileHandler", "DataHandler");
            call.setTargetEndpointAddress(url);
            call.setOperationName(new QName("FileController", "downloadFile"));
            call.registerTypeMapping(DataHandler.class, qn, JAFDataHandlerSerializerFactory.class, JAFDataHandlerDeserializerFactory.class); //
            //call.addParameter("source", XMLType.XSD_STRING ,ParameterMode.IN); //设置服务调用方法的传入参数类型
            call.setReturnType(qn, DataHandler.class);//需要返回的QName以及返回的类型是DataHandler.class
            handler = (DataHandler) call.invoke(new Object[]{});
            input = handler.getInputStream();
            fos = new FileOutputStream(file);
            byte[] buffer = new byte[1024];
            while (input.read(buffer) != -1) {
                fos.write(buffer);
            }
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (Exception e2) {      // TODO: handle exception
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (Exception e2) {      // TODO: handle exception
                }
            }
        }
    }
}

上传测试类:

package com.hello.weChat.controller;

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;

/**
 * Created by winkey on 15-2-10.
 */
public class uploadTest {
    public static void main(String[] args) throws ServiceException, RemoteException {
        String url = "http://192.168.129.245:8080/btm-szny/services/FileController";
        String fileName = "head.jpg";
        String path = "f:/test/" + 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:FileHandler", "DataHandler");
        call.registerTypeMapping(DataHandler.class, qn,
                JAFDataHandlerSerializerFactory.class,
                JAFDataHandlerDeserializerFactory.class);
        call.setOperationName(new QName(url, "upload"));

        //设置方法形参,注意的是参数1的type的DataHandler类型的,和上面的qn的类型是一样的
        call.addParameter("dataHandler", qn, 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_head.jpg" });
        System.out.println(result);
    }
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。