`
kalogen
  • 浏览: 868104 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

WebService大讲堂之Axis2(4):二进制文件传输

    博客分类:
  • Java
 
阅读更多

 在《WebService大讲堂之Axis2(2):复合类型数据的传递》中讲过,如果要传递二进制文件(如图像、音频文件等),可以使用byte[]作为数据类型进行传递,然后客户端使用RPC方式进行调用。这样做只是其中的一种方法,除此之外,在客户端还可以使用wsdl2java命令生成相应的stub类来调用WebService,wsdl2java命令的用法详见《WebService大讲堂之Axis2(1):用POJO实现0配置的WebService》。

    WebService类中包含byte[]类型参数的方法在wsdl2java生成的stub类中对应的数据类型不再是byte[]类型,而是javax.activation.DataHandler。DataHandler类是专门用来映射WebService二进制类型的。

    在WebService类中除了可以使用byte[]作为传输二进制的数据类型外,也可以使用javax.activation.DataHandler作为数据类型。 不管是使用byte[],还是使用javax.activation.DataHandler作为WebService方法的数据类型,使用wsdl2java命令生成的stub类中相应方法的类型都是javax.activation.DataHandler。而象使用.net、delphi生成的stub类的相应方法类型都是byte[]。这是由于javax.activation.DataHandler类是Java特有的,对于其他语言和技术来说,并不认识javax.activation.DataHandler类,因此,也只有使用最原始的byte[]了。

    下面是一个上传二进制文件的例子,WebService类的代码如下:

  1. package service;

  2. import java.io.InputStream;
  3. import java.io.OutputStream;
  4. import java.io.FileOutputStream;
  5. import javax.activation.DataHandler;

  6. public class FileService
  7. {
  8.   //  使用byte[]类型参数上传二进制文件
  9.     public boolean uploadWithByte(byte[] file, String filename)
  10.     {
  11.         FileOutputStream fos = null;
  12.         try
  13.         {                         
  14.             fos = new FileOutputStream(filename);   
  15.             fos.write(file);
  16.             fos.close();
  17.         }
  18.         catch (Exception e)
  19.         {
  20.             return false;
  21.         }
  22.         finally
  23.         {
  24.             if (fos != null)
  25.             {
  26.                 try
  27.                 {
  28.                     fos.close();
  29.                 }
  30.                 catch (Exception e)
  31.                 {
  32.                 }
  33.             }
  34.         }
  35.         return true;
  36.     }
  37.     private void writeInputStreamToFile(InputStream is, OutputStream os) throws Exception
  38.     {
  39.         int n = 0;
  40.         byte[] buffer = new byte[8192];
  41.         while((n = is.read(buffer)) > 0)
  42.         {
  43.             os.write(buffer, 0, n);
  44.         }
  45.     }
  46.     //  使用DataHandler类型参数上传文件
  47.     public boolean uploadWithDataHandler(DataHandler file, String filename)
  48.     {
  49.        
  50.         FileOutputStream fos = null;
  51.         try
  52.         {           
  53.             fos = new FileOutputStream(filename); 
  54.             //  可通过DataHandler类的getInputStream方法读取上传数据
  55.             writeInputStreamToFile(file.getInputStream(), fos);
  56.             fos.close();
  57.         }
  58.         catch (Exception e)
  59.         {
  60.             return false;
  61.         }
  62.         finally
  63.         {
  64.             if (fos != null)
  65.             {
  66.                 try
  67.                 {
  68.                     fos.close();
  69.                 }
  70.                 catch (Exception e)
  71.                 {
  72.                 }
  73.             }
  74.         }
  75.         return true;
  76.     }
  77. }
复制代码

上面代码在services.xml文件的配置代码如下:

  1. <service name="fileService">
  2.     <description>
  3.         文件服务
  4.     </description>
  5.     <parameter name="ServiceClass">
  6.         service.FileService
  7.     </parameter>
  8.         <messageReceivers>
  9.         <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
  10.             class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
  11.     </messageReceivers>
  12. </service>
复制代码

如果使用wsdl2java命令生成调用Java客户端代码,则需要创建DataHandler类的对象实例,代码如下:

DataHandler dh = new DataHandler(new FileDataSource(imagePath));

wsdl2java命令会为每一个方法生成一个封装方法参数的类,类名为方法名(第一个字符大写),如uploadWithByte方法生成的类名为UploadWithByte。如果要设置file参数的值,可以使用UploadWithByte类的setFile方法,代码如下:

  1. UploadWithByte uwb = new UPloadWithByte();
  2. uwb.setFile(dh);
复制代码

最后是调用uploadWithByte方法,代码如下(FileServiceStub为wsdl2java生成的stub类名):

  1. FileServiceStub fss = new FileServiceStub();
  2. fss.uploadWithByte(uwb);
复制代码

如果使用C#调用FileService,则file参数类型均为byte[],代码如下:

  1. MemoryStream ms = new MemoryStream();
  2. Bitmap bitmap = new Bitmap(picUpdateImage.Image);
  3. bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
  4. service.fileService fs = new WSC.service.fileService();
  5. fs.uploadWithDataHandler(ms.ToArray());
  6. fs.uploadWithByte(ms.ToArray());
  7. 其中picUpdateImage为c#中加载图像文件的picturebox控件
复制代码
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics