程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> WebService大講堂之Axis2(4):二進制文件傳輸

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類的代碼如下:

package service;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import javax.activation.DataHandler;
public class FileService
{
   //  使用byte[]類型參數上傳二進制文件
    public boolean uploadWithByte(byte[] file, String filename)
    {
         FileOutputStream fos = null;
         try
         {                          
             fos = new FileOutputStream(filename);    
             fos.write(file);
             fos.close();
         }
         catch (Exception e)
         {
             return false;
         }
         finally
         {
             if (fos != null)
             {
                 try
                 {
                     fos.close();
                 }
                 catch (Exception e)
                 {
                 }
             }
         }
         return true;
    }
    private void writeInputStreamToFile(InputStream is, OutputStream os) throws Exception
    {
         int n = 0;
         byte[] buffer = new byte[8192];
         while((n = is.read(buffer)) > 0)
         {
             os.write(buffer, 0, n);
         }
    }
    //  使用DataHandler類型參數上傳文件
    public boolean uploadWithDataHandler(DataHandler file, String filename)
    {
        
         FileOutputStream fos = null;
         try
         {            
             fos = new FileOutputStream(filename);   
             //  可通過DataHandler類的getInputStream方法讀取上傳數據
             writeInputStreamToFile(file.getInputStream(), fos);
             fos.close();
         }
         catch (Exception e)
         {
             return false;
         }
         finally
         {
             if (fos != null)
             {
                 try
                 {
                     fos.close();
                 }
                 catch (Exception e)
                 {
                 }
             }
         }
         return true;
    }
}

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

<service name="fileService">
    <description>
        文件服務
    </description>
    <parameter name="ServiceClass">
        service.FileService 
    </parameter>
        <messageReceivers>
        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
            class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
    </messageReceivers>
</service>

如果使用wsdl2java命令生成調用Java客戶端代碼,則需要創建DataHandler類的對象實例,代碼如下 :

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

wsdl2java命令會為每一個方法生成一個封裝方法參數的類,類名為方法名(第一個字符大寫),如 uploadWithByte方法生成的類名為UploadWithByte。如果要設置file參數的值,可以使用UploadWithByte 類的setFile方法,代碼如下:

UploadWithByte uwb=new UPloadWithByte();
uwb.setFile(dh);

最後是調用uploadWithByte方法,代碼如下(FileServiceStub為wsdl2java生成的stub類名):

FileServiceStub fss=new FileServiceStub();
fss.uploadWithByte(uwb);

如果使用C#調用FileService,則file參數類型均為byte[],代碼如下:

MemoryStream ms = new MemoryStream();
Bitmap bitmap = new Bitmap(picUpdateImage.Image);
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
service.fileService fs = new WSC.service.fileService();
fs.uploadWithDataHandler(ms.ToArray());
fs.uploadWithByte(ms.ToArray());

其中picUpdateImage為c#中加載圖像文件的picturebox控件。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved