zoukankan      html  css  js  c++  java
  • webservice文件上传下载(byte[] 实现方式)

    测试环境:axis2-1.6.1、6.0.20、jdk1.5

    说明:本方式仅适用于文件小于10M的场景(否则会出现内存溢出),大文件的上传下载应另选其他方式。

    1、创建要发布成webservice的java类。

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class BlobService {
    
        /*
         * 文件上传服务
         */
        public boolean uploadFile(String fileName,byte[] bytes)
        {
            FileOutputStream fos = null;
            try{
                fos = new FileOutputStream("F:\"+fileName);
                
                //将字节数组bytes中的数据,写入文件输出流fos中
                fos.write(bytes);
                fos.flush();
            }catch (Exception e){
                e.printStackTrace();
                return false;
            }finally{
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }      
            }
            return true;
        }
        /*
         * 文件下载服务
         */
        public byte[] downloadFile()
        {
            String filepath = "F:\head.jpg";
            FileInputStream in = null;
            byte bytes[] = null;
            try {
                in = new FileInputStream(filepath);
                bytes = new byte[in.available()];
                
                //从输入流in中,将 bytes.length 个字节的数据读入字节数组bytes中
                in.read(bytes);
            } catch (Exception e) {
                e.printStackTrace();
            }finally{        
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return bytes;
        }
    }

    2、将上面的java类编译后的class文件放到axis2WEB-INFpojo目录下。

    3、编写客户端程序。

    package client;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.util.Date;
    import javax.xml.namespace.QName;
    import org.apache.axis2.addressing.EndpointReference;
    import org.apache.axis2.client.Options;
    import org.apache.axis2.rpc.client.RPCServiceClient;
    
    public class BlobRPCClient
    {
        public static void main(String[] args) throws Exception
        {
            RPCServiceClient serviceClient = new RPCServiceClient();
            Options options = serviceClient.getOptions();
            EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2/services/BlobService");
            options.setTo(targetEPR);
           
            //=================测试文件上传==================================
            
            String filePath = "f:\head.jpg";
            FileInputStream fis = new FileInputStream(filePath);
           
            // 创建保存要上传的图像文件内容的字节数组
            byte[] buffer = new byte[fis.available()];
            
            //将输入流fis中的数据读入字节数组buffer中
            fis.read(buffer);  
          
            //设置入参(1、文件名,2、文件字节流数组)
            Object[] opAddEntryArgs = new Object[]{"我是上传的文件.jpg", buffer};
            
            //返回值类型
            Class<?>[] classes = new Class<?>[]{ Boolean.class };
            
            //指定要调用的方法名及WSDL文件的命名空间
            QName opAddEntry = new QName("http://ws.apache.org/axis2","uploadFile");
            
            //关闭流
            fis.close();
         
            //执行文件上传
            System.out.println(new Date()+" 文件上传开始");
            Object returnValue = serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0];
            System.out.println(new Date()+" 文件上传结束,返回值="+returnValue);
          
            //=================测试文件下载==================================
    
            opAddEntry = new QName("http://ws.apache.org/axis2", "downloadFile");
            
            System.out.println(new Date()+" 文件下载开始");
            byte bytes[] = (byte[]) serviceClient.invokeBlocking(opAddEntry, new Object[]{}, new Class[]{byte[].class})[0];
            FileOutputStream fileOutPutStream = new FileOutputStream("F:\我是下载的文件.jpg");
           
            //将字节数组bytes中的数据,全部写入输出流fileOutPutStream中
            fileOutPutStream.write(bytes);
            fileOutPutStream.flush();
            fileOutPutStream.close();
            System.out.println(new Date()+" 文件下载完成");
        }
    }

    文章来源:http://huangqiqing123.iteye.com/blog/1454819

    好记性不如烂笔头
  • 相关阅读:
    HTTP断点续传 规格严格
    Java Shutdown 规格严格
    linux 命令源码 规格严格
    JTable调整列宽 规格严格
    linux 多CPU 规格严格
    Hello can not find git path 规格严格
    Kill 规格严格
    拜拜牛人 规格严格
    Swing 规格严格
    Debugging hangs in JVM (on AIX but methodology applicable to other platforms) 规格严格
  • 原文地址:https://www.cnblogs.com/codehello/p/6387468.html
Copyright © 2011-2022 走看看