`
hwei_344370758
  • 浏览: 20584 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Axis2上传下载文件--以byte形式

 
阅读更多
Axis2上传下载文件
Service:
package com.siven.axis2.service;

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

public class FileService
{
    // 使用byte[]类型参数上传二进制文件
    public boolean uploadWithByte(byte[] file, String filename)
    {
        FileOutputStream fos = null;
        try
        {
            fos = new FileOutputStream("c:/temp/"+filename);
            fos.write(file);
            fos.close();
        }
        catch (Exception e)
        {
            return false;
        }
        finally
        {
            if (fos != null)
            {
                try
                {
                    fos.close();
                }
                catch (Exception e)
                {
                }
            }
        }
        return true;
    }

    private void writeInputStreamToFile(InputStream is, OutputStream os) throws Exception
    {
        int n = 0;
        byte[] buffer = new byte[8192];
        while ((n = is.read(buffer)) > 0)
        {
            os.write(buffer, 0, n);
        }
    }

    // 使用DataHandler类型参数上传文件
    public boolean uploadWithDataHandler(DataHandler file, String filename)
    {
        System.out.println("======================  start");
        FileOutputStream fos = null;
        try
        {
            fos = new FileOutputStream("c:/temp/"+filename);
            // 可通过DataHandler类的getInputStream方法读取上传数据
            writeInputStreamToFile(file.getInputStream(), fos);
            fos.close();
        }
        catch (Exception e)
        {
            return false;
        }
        finally
        {
            if (fos != null)
            {
                try
                {
                    fos.close();
                }
                catch (Exception e)
                {
                }
            }
        }
        System.out.println("======================  end");
        return true;
    }
   
    public byte[] downWithByte(String fileName) throws Exception
    {
       // 打开图像文件,确定图像文件的大小
        String filePath = "c:/temp/"+fileName;
        File file = new File(filePath);
        java.io.FileInputStream fis = new java.io.FileInputStream(filePath);
        // 创建保存要上传的图像文件内容的字节数组
        byte[] buffer = new byte[(int) file.length()];
        // 将图像文件的内容读取buffer数组中
        int n = fis.read(buffer);  //n为读入的字节数
        System.out.println("文件长度:" + file.length());
        fis.close();
        return buffer;
    }
}

===================================================================================================


RPC Client:
    public static void invokeUploadWithByte() throws AxisFault
    {
        RPCServiceClient serviceClient = new RPCServiceClient();
        Options options = serviceClient.getOptions();
        EndpointReference targetEPR = new EndpointReference("http://localhost:8888/Axis2/services/fileService");
        options.setTo(targetEPR);
        DataHandler dh = new DataHandler(new FileDataSource("c:/1.png"));//要上传的文件路径
        Object[] opAddEntryArgs = new Object[]{ dh,"a.png" };//上传到服务器的文件名
        Class[] classes = new Class[]{ Boolean.class };
        QName opAddEntry = new QName("http://service.axis2.siven.com","uploadWithByte");
        System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs,classes)[0]);
    }
   
    public static void invokeUploadWithDataHandler() throws AxisFault
    {
        RPCServiceClient serviceClient = new RPCServiceClient();
        Options options = serviceClient.getOptions();
        EndpointReference targetEPR = new EndpointReference("http://localhost:8888/Axis2/services/fileService");
        options.setTo(targetEPR);
        DataHandler dh = new DataHandler(new FileDataSource("c:/1.png"));//要上传的文件路径
        Object[] opAddEntryArgs = new Object[]{ dh,"fun.png" }; //上传到服务器的文件名
        Class[] classes = new Class[]{ Boolean.class };
        QName opAddEntry = new QName("http://service.axis2.siven.com","uploadWithDataHandler");
        System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs,classes)[0]);
    }

    public static void invokeDownWithByte() throws AxisFault
    {
        RPCServiceClient serviceClient = new RPCServiceClient();
        Options options = serviceClient.getOptions();
        EndpointReference targetEPR = new EndpointReference("http://localhost:8888/Axis2/services/fileService");
        options.setTo(targetEPR);

        Object[] opAddEntryArgs = new Object[] {"a.png"};//要下载的文件名
        Class[] classes = new Class[] {byte[].class};
        QName opAddEntry = new QName("http://service.axis2.siven.com","downWithByte");
        byte[] strArray = (byte[])serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0];
        FileOutputStream fos = null;
        try
        {
            //  将下载的图像保存在c:/temp/盘的down.png文件中
            fos = new FileOutputStream("c:/temp/down.png");
            //  开始写入图像文件的字节
            fos.write(strArray);
            fos.close();
        }catch(Exception e){
         e.printStackTrace();
        }
        System.out.println("OK");   
    }



Stub  Client:

    public static void fileServiceTest() throws RemoteException
    {
        FileServiceStub fss = new FileServiceStub();
        DataHandler dh = new DataHandler(new FileDataSource(new File("c:/1.png")));//要上传的文件路径
        FileServiceStub.UploadWithByte uwb = new FileServiceStub.UploadWithByte();
        uwb.setFile(dh);
        uwb.setFilename("ccc.png");//上传到服务器的文件名
        FileServiceStub.UploadWithByteResponse res =fss.uploadWithByte(uwb);
        System.out.println(res.get_return());
       
        FileServiceStub.UploadWithDataHandler uwh = new FileServiceStub.UploadWithDataHandler();
        uwh.setFile(dh);
        uwh.setFilename("ddd.png");//上传到服务器的文件名
        FileServiceStub.UploadWithDataHandlerResponse resh = fss.uploadWithDataHandler(uwh);
        System.out.println(resh.get_return());
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics