采用web service传输超大数据

因为以前也没有做过相关的web service开发,对于Xfire也只是知道有这么一个框架。当然现在它已经变成apache基金会旗下的一个开源项目CXF。不过,现在依旧有很多公司还在用Xfireweb service 的开发,这说明它在业界一向是口碑不错的啊。 

在学习一个框架时,我强烈建议将其自带的例子部置运行几次,这样对于理解以及搭建运行环境都有很大的帮助,也不容易出错。

在这里,我主要讲解一下用Xfire开发文件上传下载的web service应用。对于文件的传输Xfire主要有两种方式。一种是将文件编码为字符串的样式进行传输,但文件大小有一定的限制且效率较低;另一种是基于MTOM协议以附件字节流的形式进行传输,基本满足现有的文件传输大小并且效率较高。 

首先,我们先把开发环境搭建好。在myeclipse新建一个web应用程序,将Xfire所需jar包复制到lib目录下,并在src目录下按照META-INF/xfire/services.xml些目录结构新建一个services.xml文件,如下图

 接下来就是真正的环境配置了,打开WebRoot/WEB-INF/web.xml进行如下配置,如图

至此,Xfire框架已经整合到我们的web应用程序中了,这样我们就可以开发我们的web service 应用了。 

对于第一种小文件传输方式开发如下:

1.首先我们先定义一个接口,因为我们暴露给客户端的就是这个接口,定义如下:

2.主要是定义文件上传与下载的两个方法,下面我们编写接口的实现类。

 

  1. package org.carrot.file1; 
  2.  
  3. import java.io.ByteArrayInputStream; 
  4. import java.io.File; 
  5. import java.io.FileInputStream; 
  6. import java.io.FileNotFoundException; 
  7. import java.io.FileOutputStream; 
  8. import java.io.IOException; 
  9. import java.io.InputStream; 
  10. import org.codehaus.xfire.util.Base64; 
  11.  
  12. public class File1Impl implements File1 
  13.     @Override 
  14.     public String downFile(String fileName) 
  15.     { 
  16.         File file = new File("F:/", fileName); 
  17.         System.out.println(file.length()); 
  18.         StringBuffer buffer = new StringBuffer(); 
  19.         InputStream in; 
  20.         try 
  21.         { 
  22.             in = new FileInputStream(file); 
  23.             byte[] buff = new byte[1024 * 1024]; 
  24.             int len = 0
  25.             while (-1 != (len = in.read(buff, 01024))) 
  26.             { 
  27.                 buffer.append(Base64.encode(buff, 0, len)); 
  28.             } 
  29.             in.close(); 
  30.         } 
  31.         catch (FileNotFoundException e1) 
  32.         { 
  33.             e1.printStackTrace(); 
  34.         } 
  35.         catch (IOException e) 
  36.         { 
  37.             e.printStackTrace(); 
  38.         } 
  39.         String fileString = buffer.toString(); 
  40.         System.out.println(fileString.length()); 
  41.         System.out.println("running"); 
  42.         return fileString; 
  43.     } 
  44.     @Override 
  45.     public String uploadFile(String file, String fileName) 
  46.     { 
  47.         File loadFile = new File("F:/", fileName); 
  48.         InputStream in = new ByteArrayInputStream(Base64.decode(file)); 
  49.         byte[] buffer = new byte[1024 * 1024]; 
  50.         int len = -1
  51.         try 
  52.         { 
  53.             FileOutputStream out = new FileOutputStream(loadFile); 
  54.             while (-1 != (len = in.read(buffer, 0, buffer.length))) 
  55.             { 
  56.                 out.write(buffer, 0, len); 
  57.             } 
  58.             in.close(); 
  59.             out.close(); 
  60.         } 
  61.         catch (FileNotFoundException e) 
  62.         { 
  63.             e.printStackTrace(); 
  64.             return "file not found"
  65.         } 
  66.         catch (IOException e) 
  67.         { 
  68.             e.printStackTrace(); 
  69.             return "error"
  70.         } 
  71.         return "success"
  72.     } 

3.接下来就是我们对外发布服务信息的配置了,打开services.xml文件进行如下配置

这里的name指发布的服务名,namespace指命名空间用于区别,serviceClass指发布的接口,implementationClass指实现类,具体还有很多元素可以当阅相关文档,这里只列出几个我们常用的。

4.最后我们只要将这个应用部署到服务器上发布,这里我用的是Tomcat6.x进行部署,如下

5.这时我们就可以启动Tomcat服务器进行一下访问测试了,在浏览器中输入网址:http://localhost:8080/MyFile/services

6.点击进入wsdl就可以看到我们发布的服务信息了

7.这样我们的服务就发布成功了,接下来就是编写客户端进行服务的调用看是否成功。在这里客户端既可以是java web应用,也可以是java一般应用,为了jar包好导入以下皆采用java web应用作为客户端调用。客户端调用我们的服务也有三种方式:动态方式(反射)、代理方式、客户端桩方式。

注意:这里的三种方式是在java的环境下笔者所知的开发方式,在其它语言环境下开发并不是很了解,只知道C#也是采用客户端桩方式。

动态方式:客户端采用动态方式要加入Xfire相关jar包就能调用服务的方法,类似java反射

为了简单,调用服务我都写在一个main方法中,如下

  1. package org.carrot.client2; 
  2.  
  3. import java.io.File; 
  4. import java.io.FileInputStream; 
  5. import java.io.FileNotFoundException; 
  6. import java.io.FileOutputStream; 
  7. import java.io.IOException; 
  8. import java.io.InputStream; 
  9. import java.net.URL; 
  10. import org.codehaus.xfire.client.Client; 
  11. import org.codehaus.xfire.transport.http.HttpTransport; 
  12. import org.codehaus.xfire.util.Base64; 
  13.  
  14. public class Client2 
  15.     public static void main(String[] args) 
  16.     { 
  17.         try 
  18.         { 
  19.             URL myURL = new URL( 
  20.                     "http://localhost:8080/MyFile/services/File1?wsdl"); 
  21.             Client client = new Client(myURL); 
  22.  
  23.             // 文件下载 
  24.              Object[] result = client.invoke("downFile"new Object[] 
  25.              { "java6webservice.doc" }); 
  26.              String fileName = "java6webservice.doc";            
  27.              File file = new File(fileName);         
  28.              byte[] bytes = Base64.decode((String) result[0]); 
  29.              System.out.println(bytes.length);   
  30.              file.createNewFile();       
  31.              FileOutputStream out = new FileOutputStream(file); 
  32.              out.write(bytes); 
  33.              out.flush(); 
  34.              out.close(); 
  35.  
  36.             // 文件上传 
  37. //          String fileName2 = "java6webservice.doc"; 
  38. //          File file2 = new File("E:/", fileName2); 
  39. //          System.out.println(file2.length()); 
  40. //          StringBuffer sb = new StringBuffer(); 
  41. //          InputStream is = new FileInputStream(file2); 
  42. //          byte[] buff = new byte[1024 * 1024]; 
  43. //          int len = -1; 
  44. //          while (-1 != (len = is.read(buff, 0, buff.length))) 
  45. //          { 
  46. //              sb.append(Base64.encode(buff, 0, len)); 
  47. //          } 
  48. //          is.close(); 
  49. //          String fileString = sb.toString(); 
  50. //          System.out.println(fileString.length()); 
  51. //          Object[] result = client.invoke("uploadFile", new Object[] 
  52. //          { fileString, fileName2}); 
  53. //          System.out.println(result[0]); 
  54.         } 
  55.         catch (FileNotFoundException e) 
  56.         { 
  57.             e.printStackTrace(); 
  58.         } 
  59.         catch (IOException e) 
  60.         { 
  61.             e.printStackTrace(); 
  62.         } 
  63.         catch (Exception e) 
  64.         { 
  65.             e.printStackTrace(); 
  66.         } 
  67.     } 

代理方式:需要加入Xfire相关jar包,并且客户端必须提供与服务端一样的接口,即我们的服务暴露给客户端的接口,包名也最好与服务端的一样。服务调用如下

  1. package org.carrot.client3; 
  2.  
  3. import java.io.ByteArrayInputStream; 
  4. import java.io.File; 
  5. import java.io.FileInputStream; 
  6. import java.io.FileNotFoundException; 
  7. import java.io.FileOutputStream; 
  8. import java.io.IOException; 
  9. import java.io.InputStream; 
  10. import java.net.MalformedURLException; 
  11.  
  12. import org.codehaus.xfire.client.Client; 
  13. import org.codehaus.xfire.client.XFireProxyFactory; 
  14. import org.codehaus.xfire.service.Service; 
  15. import org.codehaus.xfire.service.binding.ObjectServiceFactory; 
  16. import org.codehaus.xfire.util.Base64; 
  17.  
  18. public class Client3 
  19.     public static void main(String[] args) 
  20.     { 
  21.         Service serviceModel = new ObjectServiceFactory().create(File1.class
  22.                 "File1""http://file1.carrot.org/File1"null); 
  23.         File1 service = null
  24.         try 
  25.         { 
  26.             service = (File1) new XFireProxyFactory().create(serviceModel, 
  27.                     "http://localhost:8080/MyFile/services/File1"); 
  28.         } 
  29.         catch (MalformedURLException e) 
  30.         { 
  31.             // TODO Auto-generated catch block 
  32.             e.printStackTrace(); 
  33.         } 
  34.          
  35.         //文件下载 
  36. //      String fileName1 = "java6webservice.doc"; 
  37. //      File file1 = new File("E:/", fileName1); 
  38. //      InputStream in = new ByteArrayInputStream(Base64 
  39. //              .decode(service 
  40. //                      .downFile(fileName1))); 
  41. //      byte[] buffer =  new byte[1024*1024]; 
  42. //      try 
  43. //      { 
  44. //          int len1 = -1; 
  45. //          file1.createNewFile(); 
  46. //          FileOutputStream out = new FileOutputStream(file1); 
  47. //          while(-1 != (len1 = in.read(buffer, 0, buffer.length))) 
  48. //          { 
  49. //              out.write(buffer, 0 , len1); 
  50. //          } 
  51. //          out.flush(); 
  52. //          out.close(); 
  53. //      } 
  54. //      catch (FileNotFoundException e) 
  55. //      { 
  56. //          e.printStackTrace(); 
  57. //      } 
  58. //      catch (IOException e) 
  59. //      { 
  60. //          e.printStackTrace(); 
  61. //      } 
  62. //      System.out.println(file1.length()); 
  63.          
  64.         //文件 上传 
  65.         String fileName2 = "java6webservice.doc"
  66.         File file2 = new File("E:/", fileName2); 
  67.         System.out.println(file2.length()); 
  68.         StringBuffer sb = new StringBuffer(); 
  69.         InputStream is; 
  70.         try 
  71.         { 
  72.             is = new FileInputStream(file2); 
  73.             byte[] buff = new byte[1024 * 1024]; 
  74.             int len = -1
  75.             while (-1 != (len = is.read(buff, 0, buff.length))) 
  76.             { 
  77.                 sb.append(Base64.encode(buff, 0, len)); 
  78.             } 
  79.             is.close(); 
  80.         } 
  81.         catch (FileNotFoundException e1) 
  82.         { 
  83.             e1.printStackTrace(); 
  84.         } 
  85.         catch (IOException e) 
  86.         { 
  87.             e.printStackTrace(); 
  88.         } 
  89.         String fileString = sb.toString(); 
  90.         System.out.println(fileString.length()); 
  91.         service.uploadFile(fileString, fileName2); 
  92.     } 

客户端桩方式:也是最容易理解的一种方式,它通过工具自动生成客户端代码,在调用服务时就好像在本地方法一样,同样需要加入Xfire相关jar包的支持。

自动生成工具有很多,myeclipse自带就有。这里我采用的是官方给出的例子,用Ant工具来进行生成。我们要在客户端根路径下新建一个build.xml文件,如图

打开build.xml文件,配置我们要生成的信息,如下

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <project default="genfiles" basedir="."> 
  3.     <property name="lib" value="WebRoot/WEB-INF/lib" /> 
  4.     <path id="myclasspath"> 
  5.         <fileset dir="${lib}"> 
  6.             <include name="*.jar" /> 
  7.         </fileset> 
  8.         <pathelement location="${genfiles}" /> 
  9.     </path> 
  10.     <property name="code_path" value="src" /> 
  11.     <propertynamepropertyname="wsdl_path" value="http://localhost:8080/MyFile/services/File1?wsdl" /> 
  12.     <property name="code_package" value="org.carrot.client1" /> 
  13.     <target name="genfiles" description="Generate the files"> 
  14.         <taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask" classpathref="myclasspath" /> 
  15.         <wsgen outputDirectory="${code_path}" wsdl="${wsdl_path}" package="${code_package}" binding="xmlbeans" overwrite="true" /> 
  16.     </target> 
  17. </project> 

我们运行此文件就会在src目录下生成一堆称之为桩的文件,如下图

大家可以看到在包org.carrot.client1下生成了三个文件File1Client.javaFile1Impl.javaFile1PortType.java。这里的Client1.java是我接下来要讲的我写的调用服务的测试类。有了这几个文件我们与服务端打交道就轻松多了,不信你看

  1. package org.carrot.client1; 
  2.  
  3. import java.io.ByteArrayInputStream; 
  4. import java.io.File; 
  5. import java.io.FileInputStream; 
  6. import java.io.FileNotFoundException; 
  7. import java.io.FileOutputStream; 
  8. import java.io.IOException; 
  9. import java.io.InputStream; 
  10. import org.codehaus.xfire.util.Base64; 
  11.  
  12. public class Client1 
  13.     public static void main(String[] args) 
  14.     { 
  15.         File1Client client = new File1Client(); 
  16.         File1PortType service = client.getFile1HttpPort(); 
  17.          
  18.         //文件下载 
  19. //      String fileName1 = "java6webservice.doc"; 
  20. //      File file1 = new File("E:/", fileName1); 
  21. //      InputStream in = new ByteArrayInputStream(Base64 
  22. //              .decode(service 
  23. //                      .downFile(fileName1))); 
  24. //      byte[] buffer =  new byte[1024*1024]; 
  25. //      try 
  26. //      { 
  27. //          int len1 = -1; 
  28. //          file1.createNewFile(); 
  29. //          FileOutputStream out = new FileOutputStream(file1); 
  30. //          while(-1 != (len1 = in.read(buffer, 0, buffer.length))) 
  31. //          { 
  32. //              out.write(buffer, 0 , len1); 
  33. //          } 
  34. //          out.flush(); 
  35. //          out.close(); 
  36. //      } 
  37. //      catch (FileNotFoundException e) 
  38. //      { 
  39. //          e.printStackTrace(); 
  40. //      } 
  41. //      catch (IOException e) 
  42. //      { 
  43. //          e.printStackTrace(); 
  44. //      } 
  45. //      System.out.println(file1.length()); 
  46.          
  47.          
  48.         //文件 上传 
  49.         String fileName2 = "java6webservice.doc"
  50.         File file2 = new File("E:/", fileName2); 
  51.         System.out.println(file2.length()); 
  52.         StringBuffer sb = new StringBuffer(); 
  53.         InputStream is; 
  54.         try 
  55.         { 
  56.             is = new FileInputStream(file2); 
  57.             byte[] buff = new byte[1024 * 1024]; 
  58.             int len = -1
  59.             while (-1 != (len = is.read(buff, 0, buff.length))) 
  60.             { 
  61.                 sb.append(Base64.encode(buff, 0, len)); 
  62.             } 
  63.             is.close(); 
  64.         } 
  65.         catch (FileNotFoundException e1) 
  66.         { 
  67.             e1.printStackTrace(); 
  68.         } 
  69.         catch (IOException e) 
  70.         { 
  71.             e.printStackTrace(); 
  72.         } 
  73.         String fileString = sb.toString(); 
  74.         System.out.println(fileString.length()); 
  75.         service.uploadFile(fileString, fileName2);   
  76.     } 

上面的测试类调用服务,我们只写了两行代码就搞定了是不是比前面两种方式简单多了。经本人测试这三种方式都能够成功的运行。但是我们从代码上可以分析出,这三种方式的文件传输本质上都是一样的。就是将文件编码为字符串的形式再传出去,再深入一点也就是将文件整个读入内存中然后一并发出去。如果文件太大就会报内存溢出的问题,并且对于字符串编码的转换也是非常耗时的。所以这种方式只能传一些小文件,大文件就不行了。下面我就要讲的是Xfire支持的一种优化的支持大数据二进制流方式传输文件的技术MTOM,在Xfire自带的例子中已有很详细的介绍了。

对于第二种大文件传输方式开发如下:

注意:用MTOM时建议大家用java ee6的库,因数java ee5库中有个mail包冲突错误。

1.第二种方式,主要针对大数据以二进制流进行文件的传输,主要支持byte[]DataSourceDataHandler三种数据类型。这里我选用DataHandler进行讲解,同样我们要定义一个接口以及实现类,如下:

接口:

实现类:

  1. package org.file.service; 
  2.  
  3. import java.io.File; 
  4. import java.io.FileOutputStream; 
  5. import java.io.IOException; 
  6. import java.io.InputStream; 
  7.  
  8. import javax.activation.DataHandler; 
  9. import javax.activation.FileDataSource; 
  10.  
  11. public class MyFileImpl implements MyFile 
  12.     @Override 
  13.     public String uploadFile(DataHandler dh, String fileName) 
  14.     { 
  15.         long startTime = System.currentTimeMillis(); 
  16.         File file = new File("E://", fileName); 
  17.         try 
  18.         { 
  19.             file.createNewFile(); 
  20.             InputStream in = dh.getInputStream(); 
  21.             FileOutputStream out = new FileOutputStream(file); 
  22.             byte[] buff = new byte[1024 * 1024]; 
  23.             int len = -1
  24.             while (-1 != (len = in.read(buff, 0, buff.length))) 
  25.             { 
  26.                 out.write(buff, 0, len); 
  27.             } 
  28.             in.close(); 
  29.             out.flush(); 
  30.             out.close(); 
  31.         } 
  32.         catch (IOException e) 
  33.         { 
  34.             e.printStackTrace(); 
  35.             return "error"
  36.         } 
  37.         long endTime = System.currentTimeMillis(); 
  38.         System.out.println((endTime - startTime)/1000 + " s"); 
  39.         return "success"
  40.     } 
  41.  
  42.     @Override 
  43.     public DataHandler downFile(String fileName) 
  44.     { 
  45.         File file = new File("F://", fileName); 
  46.         System.out.println(file.length()); 
  47.         DataHandler dh = new DataHandler(new FileDataSource(file)); 
  48.         return dh; 
  49.     } 

2.基本的配置方式与第一种方式差不多,但是要用到MTOM技术,所以我们要在services.xml中增加一个元素,如下

3.接下来在Tomcat上部署发布都与方式一是一样的,这里就不多说了。

4.好了这里重点讲的是如何开发客户端调用基于MTOM的应用。也许你会问是否也有三种方式,我想说官方的例子只给出了用代理方式,本人也经过大量测式另外两种方式都会出现一些莫名的问题。比如MTOM支持的数据类型,生成的客户端代码都会变成DataHandler类型。

所以,我推荐大家还是用代理方式进行客户端的开发,客户端必须提供与服务端一样的接口(即我们的服务暴露给客户端的接口),包名也最好与服务端的一样,不然会出现问题。这里的客户端写法也与第一种方式有些差别,代码如下

  1. package org.file.service; 
  2.  
  3. import java.io.File; 
  4. import java.io.FileOutputStream; 
  5. import java.io.IOException; 
  6. import java.io.InputStream; 
  7. import java.net.MalformedURLException; 
  8.  
  9. import javax.activation.DataHandler; 
  10. import javax.activation.FileDataSource; 
  11.  
  12. import org.codehaus.xfire.client.Client; 
  13. import org.codehaus.xfire.client.XFireProxyFactory; 
  14. import org.codehaus.xfire.service.Service; 
  15. import org.codehaus.xfire.service.binding.ObjectServiceFactory; 
  16. import org.codehaus.xfire.transport.http.HttpTransport; 
  17.  
  18. public class MyTest 
  19.     public static void main(String[] args) throws MalformedURLException 
  20.     { 
  21.         Service serviceModel = new ObjectServiceFactory().create(MyFile.class
  22.                 "FileService""http://service.file.org/FileService"null); 
  23.  
  24.         MyFile service = (MyFile) new XFireProxyFactory().create(serviceModel, 
  25.                 "http://localhost:8080/MyFileService/services/FileService"); 
  26.  
  27.         // 增加的代码,表示采用MTOM方式处理 
  28.         Client client = Client.getInstance(service); 
  29.         client.setProperty("mtom-enabled""true"); 
  30.         client.setProperty(HttpTransport.CHUNKING_ENABLED, "true"); 
  31.  
  32.         long startTime = System.currentTimeMillis(); 
  33.  
  34.         //文件上传 
  35.         String fileName = "ZGC_CD_2011V4.1.iso"
  36.         File file = new File("F:/", fileName); 
  37.         DataHandler dh = new DataHandler(new FileDataSource(file)); 
  38.         System.out.println(service.uploadFile(dh, fileName)); 
  39.  
  40.          
  41.         //文件下载 
  42. //      File file1 = new File("E://", fileName); 
  43. // 
  44. //      DataHandler dh = service.downFile(fileName); 
  45. // 
  46. //      try 
  47. //      { 
  48. //          file1.createNewFile(); 
  49. //          InputStream in = dh.getInputStream(); 
  50. //          FileOutputStream out = new FileOutputStream(file1); 
  51. //          byte[] buff = new byte[1024 * 1024]; 
  52. //          int len = -1; 
  53. //          while (-1 != (len = in.read(buff, 0, buff.length))) 
  54. //          { 
  55. //              out.write(buff, 0, len); 
  56. //          } 
  57. //          in.close(); 
  58. //          out.flush(); 
  59. //          out.close(); 
  60. //          System.out.println("down over"); 
  61. //      } 
  62. //      catch (IOException e) 
  63. //      { 
  64. //          e.printStackTrace(); 
  65. //      } 
  66.  
  67.         long endTime = System.currentTimeMillis(); 
  68.         System.out.println("time: " + (endTime - startTime) / 1000 + " s"); 
  69.     } 

上面的代码与第一种方式写法大同小异,只是多了三行代码表示客户端采用MTOM方式处理数据类型。     

  1. Client client = Client.getInstance(service); 
  2.  
  3. client.setProperty("mtom-enabled""true"); 
  4.  
  5. client.setProperty(HttpTransport.CHUNKING_ENABLED, "true"); 

至此,客户端也就写完了,经测试1G的文件没有问题。

 

 

希望对大家有帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值