dubbo处理文件上传

dubbo和hessian的maven依赖:

 

		
			com.alibaba
			dubbo
			2.5.3
		
		
			com.caucho
			hessian
			4.0.7
		

 

 

 

服务提供者(项目名称:provider)

 

首先是web.xml配置(使用spring):

 



	
		contextConfigLocation
		classpath*:applicationContext.xml
	
	
		org.springframework.web.context.ContextLoaderListener
	
	
		dubbo
		com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet
		1
	
	
		dubbo
		/*
	


 

 

 

然后是服务接口:

 

package com.tch.test.dubbo.service;

import java.io.InputStream;

public interface DemoService {
	
    String sayHello(String name);

    /**
     * 处理上传
     * @param in
     * @return
     */
    String upload(String destPath,InputStream in);


}

 

 

服务实现类:

 

package com.tch.test.dubbo.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

public class DemoServiceImpl implements DemoService {

    public String sayHello(String name) {
        return "Hello " + name;
    }

	@Override
	public String upload(String destPath,InputStream in) {
		try {
			FileOutputStream out = new FileOutputStream("E:\\temp\\a.js");
			int n = -1;
			byte[] b = new byte[10240];
			while((n=in.read(b)) != -1){
				System.out.println(new String(b,0,n,"utf-8"));
				out.write(b, 0, n);
			}
			out.close();
			out.flush();
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "upload complete !";
	}

}

 

 

最重要的applicationContext.xml :

 

参考: dubbo hessian协议

 


	
    
    
    
    
    
    
    
    
    
	

 

启动项目之后,就同时启动了服务。。。

 

 

 

 

下面是 consumer :

 

首先是服务接口:

 

package com.tch.test.dubbo.service;

import java.io.InputStream;

public interface DemoService {
	
    String sayHello(String name);

    /**
     * 上传
     * @param in
     */
    String upload(String destPath,InputStream in);
    
}

 

 

 

然后是applicationContext.xml :

 



	
	

	
	

	
	

 

 

好了,开始调用provider提供的服务:

 

package com.tch.test.dubbo.service;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Consumer {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
        context.start();
        DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理
        sayHello(demoService);  //sayHello
        upload(demoService);    //upload
        
        context.close();
    }
    //调用sayHello
    public static void sayHello(DemoService demoService){
    	String result = demoService.sayHello(" dubbo "); // 执行远程方法
        System.out.println(result); // 显示调用结果
    }
    //调用upload
    public static void upload(DemoService demoService) throws FileNotFoundException{
    	String result = demoService.upload("E:\\temp\\a.js",new FileInputStream("E:\\temp\\dubbo.xml")); // 执行远程方法
        System.out.println(result); // 显示调用结果
    }
    

}

 

 

 

就可以看到上传成功的结果了。。。

你可能感兴趣的:(dubbo)