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. }  
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;

/*
 * DataHandler处理方式
 */
public class BlobService2 {

	/*
	 * 文件上传服务
	 */
    public boolean uploadFile(String fileName,DataHandler dataHandler)
    {
        OutputStream os = null;
        try{
        	os = new FileOutputStream("F:\\"+fileName);
            dataHandler.writeTo(os);//大附件也会出现内存溢出
            os.flush();
        }catch (Exception e){
        	e.printStackTrace();
            return false;
        }finally{
        	try {
				os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}  	
        }
        return true;
    }
    /*
     * 文件下载服务
     */
    public DataHandler downloadFile()
    {
    	String filepath = "F:\\head.jpg";
    	DataHandler dataHandler = new DataHandler(new FileDataSource(filepath));
    	return dataHandler;
    }
}

 

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. }  
package client;

import java.io.FileOutputStream;
import java.util.Date;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

/*
 * 仅适用于小附件上传、下载,10M以下。
 */
public class BlobRPCClient2
{
    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";
        DataHandler dataHandler = new DataHandler(new FileDataSource(filePath));
      
        //设置入参(1、文件名,2、DataHandler)
        Object[] opAddEntryArgs = new Object[]{"我是上传的文件.jpg", dataHandler};
        
        //返回值类型
        Class<?>[] classes = new Class<?>[]{ Boolean.class };
        
        //指定要调用的方法名及WSDL文件的命名空间
        QName opAddEntry = new QName("http://ws.apache.org/axis2","uploadFile");
     
        //执行文件上传
        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");
        opAddEntryArgs = new Object[]{};
        classes =  new Class<?>[]{ DataHandler.class };
        
        System.out.println(new Date()+" 文件下载开始");
        DataHandler returnHandler = (DataHandler) serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0];
        FileOutputStream fileOutPutStream = new FileOutputStream("F:\\我是下载的文件.jpg");
        returnHandler.writeTo(fileOutPutStream);
        fileOutPutStream.flush();
        fileOutPutStream.close();
        System.out.println(new Date()+" 文件下载完成");
    }
}

 

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 文件下载完成  
Fri Mar 16 11:48:11 CST 2012 文件上传开始
Fri Mar 16 11:48:11 CST 2012 文件上传结束,返回值=true
Fri Mar 16 11:48:11 CST 2012 文件下载开始
Fri Mar 16 11:48:12 CST 2012 文件下载完成

 

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值