天天看点

阿里巴巴dubbo处理文件上传下载 java.io.IOException: stream is closed

 注:使用InputStream 作为方法参数的时候,需要将该参数作为方法的最后一个参数,否则会有问题

dubbo和hessian的maven依赖:

Java代码  

阿里巴巴dubbo处理文件上传下载 java.io.IOException: stream is closed
  1. <dependency>  
  2.     <groupId>com.alibaba</groupId>  
  3.     <artifactId>dubbo</artifactId>  
  4.     <version>2.5.3</version>  
  5. </dependency>  
  6. <dependency>  
  7.     <groupId>com.caucho</groupId>  
  8.     <artifactId>hessian</artifactId>  
  9.     <version>4.0.7</version>  
  10. </dependency>  

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

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

Xml代码  

阿里巴巴dubbo处理文件上传下载 java.io.IOException: stream is closed
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  6.     <context-param>  
  7.         <param-name>contextConfigLocation</param-name>  
  8.         <param-value>classpath*:applicationContext.xml</param-value>  
  9.     </context-param>  
  10.     <listener>  
  11.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  12.     </listener>  
  13.     <servlet>  
  14.         <servlet-name>dubbo</servlet-name>  
  15.         <servlet-class>com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet</servlet-class>  
  16.         <load-on-startup>1</load-on-startup>  
  17.     </servlet>  
  18.     <servlet-mapping>  
  19.         <servlet-name>dubbo</servlet-name>  
  20.         <url-pattern>  
  21.     public InputStream download(String path);  
  22.         String upload(String destPath,InputStream in);  
  23. }  

服务实现类:

Java代码  

阿里巴巴dubbo处理文件上传下载 java.io.IOException: stream is closed
  1. package com.tch.test.dubbo.service;  
  2. import java.io.FileInputStream;  
  3. import java.io.InputStream;  
  4. public class DemoServiceImpl implements DemoService {  
  5.     @Override  
  6.     public InputStream download(String path) {  
  7.         System.out.println(path+"******************************");  
  8.         try {  
  9.             InputStream inputStream = new FileInputStream(path);  
  10.             return inputStream;  
  11.         } catch (Exception e) {  
  12.             e.printStackTrace();  
  13.         }  
  14.         return null;  
  15.     }  
  16.     @Override  
  17.     public String sayHello(String name) {  
  18.         return "Hello " + name;  
  19.     }  
  20.     @Override  
  21.     public String upload(String destPath,InputStream in) {  
  22.         try {  
  23.             //FileOutputStream out = new FileOutputStream("E:\\temp\\a.js");  
  24.             int n = -1;  
  25.             byte[] b = new byte[10240];  
  26.             while((n=in.read(b)) != -1){  
  27.                 System.out.println(new String(b,0,n,"utf-8"));  
  28.                 //out.write(b, 0, n);  
  29.             }  
  30.             //out.close();  
  31.             //out.flush();  
  32.             in.close();  
  33.         } catch (Exception e) {  
  34.             e.printStackTrace();  
  35.         }  
  36.         return "upload complete !";  
  37.     }  
  38. }  

最重要的applicationContext.xml :

参考:dubbo hessian协议

Xml代码  

阿里巴巴dubbo处理文件上传下载 java.io.IOException: stream is closed
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  5.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  6.                     http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd  
  7.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  8.     <!-- 提供方应用信息,用于计算依赖关系 -->  
  9.     <dubbo:application name="hello-world-app"  />  
  10.     <!-- 使用multicast广播注册中心暴露服务地址 -->  
  11.     <dubbo:registry address="multicast://224.5.6.7:1234" />  
  12.     <!-- 用hessian协议在8080(<span style="font-family: Arial, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 17.328125px; white-space: normal; background-color: #ffffff;">servlet容器的端口</span>)端口暴露服务,contextpath是项目名称,server这里是使用的web.xml里面配置的servlet -->  
  13.     <dubbo:protocol name="hessian" port="8080" contextpath="dubbo" server="servlet"/>  
  14.     <!-- 声明需要暴露的服务接口 -->  
  15.     <dubbo:service interface="com.tch.test.dubbo.service.DemoService" ref="demoService" />  
  16.     <!-- 和本地bean一样实现服务 -->  
  17.     <bean id="demoService" class="com.tch.test.dubbo.service.DemoServiceImpl" />  
  18. </beans>  

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

下面是 consumer :

首先是服务接口:

Java代码  

阿里巴巴dubbo处理文件上传下载 java.io.IOException: stream is closed
  1. package com.tch.test.dubbo.service;  
  2. public interface DemoService {  
  3.         String sayHello(String name);  
  4.     public InputStream download(String path);  
  5.         String upload(String destPath,InputStream in);  
  6. }  

然后是applicationContext.xml :

Java代码  

阿里巴巴dubbo处理文件上传下载 java.io.IOException: stream is closed
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  6.         http://code.alibabatech.com/schema/dubbo  
  7.         http://code.alibabatech.com/schema/dubbo/dubbo.xsd  
  8.         ">  
  9.     <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->  
  10.     <dubbo:application name="consumer-of-helloworld-app" />  
  11.     <!-- 使用multicast广播注册中心暴露发现服务地址 -->  
  12.     <dubbo:registry address="multicast://224.5.6.7:1234" />  
  13.     <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->  
  14.     <dubbo:reference id="demoService"  
  15.         interface="com.tch.test.dubbo.service.DemoService" />  
  16. </beans>  

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

Java代码  

阿里巴巴dubbo处理文件上传下载 java.io.IOException: stream is closed
  1. package com.alibaba.dubbo;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileNotFoundException;  
  4. import java.io.InputStream;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6. import com.alibaba.dubbo.demo.DemoService;  
  7. public class Consumer {  
  8.     public static void main(String[] args) throws Exception {  
  9.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});  
  10.         context.start();  
  11.         DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理  
  12.         sayHello(demoService);  //sayHello  
  13.         upload(demoService);    //upload  
  14.         download(demoService); //download  
  15.         context.close();  
  16.     }  
  17.     //调用sayHello  
  18.     public static void sayHello(DemoService demoService){  
  19.         String result = demoService.sayHello(" dubbo "); // 执行远程方法  
  20.         System.out.println(result); // 显示调用结果  
  21.     }  
  22.     //调用upload  
  23.     public static void upload(DemoService demoService) throws FileNotFoundException{  
  24.         String result = demoService.upload("E:\\temp\\a.js",new FileInputStream("D:\\Program files\\apache-maven-3.1.0\\repository\\com\\caucho\\hessian\\4.0.7\\hessian-4.0.7.pom")); // 执行远程方法  
  25.         System.out.println(result); // 显示调用结果  
  26.     }  
  27.     //调用upload  
  28.     public static void download(DemoService demoService) throws Exception{  
  29.         String srcPath = "D:\\Program files\\apache-maven-3.1.0\\repository\\com\\caucho\\hessian\\4.0.7\\hessian-4.0.7.pom";  
  30.         InputStream inputStream = demoService.download(srcPath); // 执行远程方法  
  31.         byte b[] = new byte[1024];  
  32.         int n;  
  33.         try {  
  34.             while ((n = inputStream.read(b)) != -1) {  
  35.                 System.out.print(new String(b,0,n,"utf-8"));  
  36.             }  
  37.         } catch (Exception e) {  
  38.             e.printStackTrace();  
  39.         }finally{  
  40.             inputStream.close();  
  41.         }  
  42.     }  
  43. }  

 如果调用到download方法的时候,出现 java.io.IOException: stream is closed    

 则此时应该是jar包的问题,貌似这是hessian的一个bug : 关于该bug

解决方法下载hessian.jar的源码包,修改 com.caucho.hessian.client.HessianProxy :

在   Object value = in.readReply(method.getReturnType()); 这一句话之后加上下面代码:

Java代码  

阿里巴巴dubbo处理文件上传下载 java.io.IOException: stream is closed
  1. if (value instanceof InputStream) {  
  2.        value = new ResultInputStream(conn, is, in, (InputStream) value);  
  3.        is = null;  
  4.        conn = null;  
  5. }  

然后重新打包,使用这个新的jar包就没问题了。。。

然后,就可以看到sayHello、下载、上传成功的结果了。。。