webservice 传输文件

来源:互联网 发布:淘宝 2.8mm 监控镜头 编辑:程序博客网 时间:2024/05/18 15:06

webservice文件上传下载(DataHandler 实现方式)

    博客分类:
  • webservice
 

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

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

 

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

Java代码
  1. import java.io.FileOutputStream;   
  2. import java.io.IOException;   
  3. import java.io.OutputStream;   
  4. import javax.activation.DataHandler;   
  5. import javax.activation.FileDataSource;   
  6.   
  7. /*  
  8.  * DataHandler处理方式  
  9.  */  
  10. public class BlobService2 {   
  11.   
  12.     /*  
  13.      * 文件上传服务  
  14.      */  
  15.     public boolean uploadFile(String fileName,DataHandler dataHandler)   
  16.     {   
  17.         OutputStream os = null;   
  18.         try{   
  19.             os = new FileOutputStream("F:\\"+fileName);   
  20.             dataHandler.writeTo(os);//大附件也会出现内存溢出  
  21.             os.flush();   
  22.         }catch (Exception e){   
  23.             e.printStackTrace();   
  24.             return false;   
  25.         }finally{   
  26.             try {   
  27.                 os.close();   
  28.             } catch (IOException e) {   
  29.                 e.printStackTrace();   
  30.             }      
  31.         }   
  32.         return true;   
  33.     }   
  34.     /*  
  35.      * 文件下载服务  
  36.      */  
  37.     public DataHandler downloadFile()   
  38.     {   
  39.         String filepath = "F:\\head.jpg";   
  40.         DataHandler dataHandler = new DataHandler(new FileDataSource(filepath));   
  41.         return dataHandler;   
  42.     }   
  43. }  

 

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

 

3、编写客户端程序。

Java代码 复制代码 收藏代码
  1. package client;   
  2.   
  3. import java.io.FileOutputStream;   
  4. import java.util.Date;   
  5. import javax.activation.DataHandler;   
  6. import javax.activation.FileDataSource;   
  7. import javax.xml.namespace.QName;   
  8. import org.apache.axis2.addressing.EndpointReference;   
  9. import org.apache.axis2.client.Options;   
  10. import org.apache.axis2.rpc.client.RPCServiceClient;   
  11.   
  12. /*  
  13.  * 仅适用于小附件上传、下载,10M以下。  
  14.  */  
  15. public class BlobRPCClient2   
  16. {   
  17.     public static void main(String[] args) throws Exception   
  18.     {   
  19.         RPCServiceClient serviceClient = new RPCServiceClient();   
  20.         Options options = serviceClient.getOptions();   
  21.         EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2/services/BlobService");   
  22.         options.setTo(targetEPR);   
  23.           
  24.         //=================测试文件上传==================================  
  25.            
  26.         String filePath = "f:\\head.jpg";   
  27.         DataHandler dataHandler = new DataHandler(new FileDataSource(filePath));   
  28.          
  29.         //设置入参(1、文件名,2、DataHandler)   
  30.         Object[] opAddEntryArgs = new Object[]{"我是上传的文件.jpg", dataHandler};   
  31.            
  32.         //返回值类型   
  33.         Class<?>[] classes = new Class<?>[]{ Boolean.class };   
  34.            
  35.         //指定要调用的方法名及WSDL文件的命名空间   
  36.         QName opAddEntry = new QName("http://ws.apache.org/axis2","uploadFile");   
  37.         
  38.         //执行文件上传   
  39.         System.out.println(new Date()+" 文件上传开始");   
  40.         Object returnValue = serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0];   
  41.         System.out.println(new Date()+" 文件上传结束,返回值="+returnValue);   
  42.          
  43.         //=================测试文件下载==================================  
  44.   
  45.         opAddEntry = new QName("http://ws.apache.org/axis2""downloadFile");   
  46.         opAddEntryArgs = new Object[]{};   
  47.         classes =  new Class<?>[]{ DataHandler.class };   
  48.            
  49.         System.out.println(new Date()+" 文件下载开始");   
  50.         DataHandler returnHandler = (DataHandler) serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0];   
  51.         FileOutputStream fileOutPutStream = new FileOutputStream("F:\\我是下载的文件.jpg");   
  52.         returnHandler.writeTo(fileOutPutStream);   
  53.         fileOutPutStream.flush();   
  54.         fileOutPutStream.close();   
  55.         System.out.println(new Date()+" 文件下载完成");   
  56.     }   
  57. }  

 

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

Java代码 复制代码 收藏代码
  1. Fri Mar 16 11:48:11 CST 2012 文件上传开始   
  2. Fri Mar 16 11:48:11 CST 2012 文件上传结束,返回值=true  
  3. Fri Mar 16 11:48:11 CST 2012 文件下载开始   
  4. Fri Mar 16 11:48:12 CST 2012 文件下载完成  

 

http://huangqiqing123.iteye.com/blog/1455169

0 0