webservice文件上传下载(byte[] 实现方式)

转载自:http://huangqiqing123.iteye.com/blog/1454819


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

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

 

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

Java代码   收藏代码
  1. import java.io.FileInputStream;  
  2. import java.io.FileOutputStream;  
  3. import java.io.IOException;  
  4.   
  5. public class BlobService {  
  6.   
  7.     /* 
  8.      * 文件上传服务 
  9.      */  
  10.     public boolean uploadFile(String fileName,byte[] bytes)  
  11.     {  
  12.         FileOutputStream fos = null;  
  13.         try{  
  14.             fos = new FileOutputStream("F:\\"+fileName);  
  15.               
  16.             //将字节数组bytes中的数据,写入文件输出流fos中  
  17.             fos.write(bytes);  
  18.             fos.flush();  
  19.         }catch (Exception e){  
  20.             e.printStackTrace();  
  21.             return false;  
  22.         }finally{  
  23.             try {  
  24.                 fos.close();  
  25.             } catch (IOException e) {  
  26.                 e.printStackTrace();  
  27.             }     
  28.         }  
  29.         return true;  
  30.     }  
  31.     /* 
  32.      * 文件下载服务 
  33.      */  
  34.     public byte[] downloadFile()  
  35.     {  
  36.         String filepath = "F:\\head.jpg";  
  37.         FileInputStream in = null;  
  38.         byte bytes[] = null;  
  39.         try {  
  40.             in = new FileInputStream(filepath);  
  41.             bytes = new byte[in.available()];  
  42.               
  43.             //从输入流in中,将 bytes.length 个字节的数据读入字节数组bytes中  
  44.             in.read(bytes);  
  45.         } catch (Exception e) {  
  46.             e.printStackTrace();  
  47.         }finally{         
  48.             try {  
  49.                 in.close();  
  50.             } catch (IOException e) {  
  51.                 e.printStackTrace();  
  52.             }  
  53.         }  
  54.         return bytes;  
  55.     }  
  56. }  

 

 

2、将上面的java类编译后的class文件放到axis2\WEB-INF\pojo目录下。

 

3、编写客户端程序。

Java代码   收藏代码
  1. package client;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.util.Date;  
  6. import javax.xml.namespace.QName;  
  7. import org.apache.axis2.addressing.EndpointReference;  
  8. import org.apache.axis2.client.Options;  
  9. import org.apache.axis2.rpc.client.RPCServiceClient;  
  10.   
  11. public class BlobRPCClient  
  12. {  
  13.     public static void main(String[] args) throws Exception  
  14.     {  
  15.         RPCServiceClient serviceClient = new RPCServiceClient();  
  16.         Options options = serviceClient.getOptions();  
  17.         EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2/services/BlobService");  
  18.         options.setTo(targetEPR);  
  19.          
  20.         //=================测试文件上传==================================  
  21.           
  22.         String filePath = "f:\\head.jpg";  
  23.         FileInputStream fis = new FileInputStream(filePath);  
  24.          
  25.         // 创建保存要上传的图像文件内容的字节数组  
  26.         byte[] buffer = new byte[fis.available()];  
  27.           
  28.         //将输入流fis中的数据读入字节数组buffer中  
  29.         fis.read(buffer);    
  30.         
  31.         //设置入参(1、文件名,2、文件字节流数组)  
  32.         Object[] opAddEntryArgs = new Object[]{"我是上传的文件.jpg", buffer};  
  33.           
  34.         //返回值类型  
  35.         Class<?>[] classes = new Class<?>[]{ Boolean.class };  
  36.           
  37.         //指定要调用的方法名及WSDL文件的命名空间  
  38.         QName opAddEntry = new QName("http://ws.apache.org/axis2","uploadFile");  
  39.           
  40.         //关闭流  
  41.         fis.close();  
  42.        
  43.         //执行文件上传  
  44.         System.out.println(new Date()+" 文件上传开始");  
  45.         Object returnValue = serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0];  
  46.         System.out.println(new Date()+" 文件上传结束,返回值="+returnValue);  
  47.         
  48.         //=================测试文件下载==================================  
  49.   
  50.         opAddEntry = new QName("http://ws.apache.org/axis2""downloadFile");  
  51.           
  52.         System.out.println(new Date()+" 文件下载开始");  
  53.         byte bytes[] = (byte[]) serviceClient.invokeBlocking(opAddEntry, new Object[]{}, new Class[]{byte[].class})[0];  
  54.         FileOutputStream fileOutPutStream = new FileOutputStream("F:\\我是下载的文件.jpg");  
  55.          
  56.         //将字节数组bytes中的数据,全部写入输出流fileOutPutStream中  
  57.         fileOutPutStream.write(bytes);  
  58.         fileOutPutStream.flush();  
  59.         fileOutPutStream.close();  
  60.         System.out.println(new Date()+" 文件下载完成");  
  61.     }  
  62. }  

 

 

4、运行客户端程序,输出结果如下:

Java代码   收藏代码
  1. Thu Mar 15 20:42:55 CST 2012 文件上传开始  
  2. Thu Mar 15 20:42:56 CST 2012 文件上传结束,返回值=true  
  3. Thu Mar 15 20:42:56 CST 2012 文件下载开始  
  4. Thu Mar 15 20:42:56 CST 2012 文件下载完成  

 

 

5、打开目录 F:\,会看到:


 

http://huangqiqing123.iteye.com/admin/blogs/1454819


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值