1. 程式人生 > >WebService大講堂之Axis2(4):二進位制檔案傳輸

WebService大講堂之Axis2(4):二進位制檔案傳輸

 

    在《WebService大講堂之Axis2(2):複合型別資料的傳遞》中講過,如果要傳遞二進位制檔案(如影象、音訊檔案等),可以使用byte[]作為資料型別進行傳遞,然後客戶端使用RPC方式進行呼叫。這樣做只是其中的一種方法,除此之外,在客戶端還可以使用wsdl2java命令生成相應的stub類來呼叫WebServicewsdl2java命令的用法詳見《WebService大講堂之Axis2(1):用POJO實現0配置的WebService》。
    WebService類中包含byte[]型別引數的方法在wsdl2java生成的stub類中對應的資料型別不再是byte[]型別,而是

javax.activation.DataHandlerDataHandler類是專門用來對映WebService二進位制型別的。
    在
WebService類中除了可以使用byte[]作為傳輸二進位制的資料型別外,也可以使用javax.activation.DataHandler作為資料型別。不管是使用byte[],還是使用javax.activation.DataHandler作為WebService方法的資料型別,使用wsdl2java命令生成的stub類中相應方法的型別都是javax.activation.DataHandler。而象使用.netdelphi生成的stub類的相應方法型別都是
byte[]。這是由於javax.activation.DataHandler類是Java特有的,對於其他語言和技術來說,並不認識javax.activation.DataHandler類,因此,也只有使用最原始的byte[]了。
    下面是一個上傳二進位制檔案的例子,
WebService類的程式碼如下:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->package service;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import javax.activation.DataHandler;

publicclass FileService
{
   
//  使用byte[]型別引數上傳二進位制檔案publicboolean uploadWithByte(byte[] file, String filename)
    {
         FileOutputStream fos 
=null;
         
try
         {                          
             fos 
=new FileOutputStream(filename);    
             fos.write(file);
             fos.close();
         }
         
catch (Exception e)
         {
             
returnfalse;
         }
         
finally
         {
             
if (fos !=null)
             {
                 
try
                 {
                     fos.close();
                 }
                 
catch (Exception e)
                 {
                 }
             }
         }
         
returntrue;
    }
    
privatevoid writeInputStreamToFile(InputStream is, OutputStream os) throws Exception
    {
         
int n =0;
         
byte[] buffer =newbyte[8192];
         
while((n = is.read(buffer)) >0)
         {
             os.write(buffer, 
0, n);
         }
    }
    
//  使用DataHandler型別引數上傳檔案publicboolean uploadWithDataHandler(DataHandler file, String filename)
    {
        
         FileOutputStream fos 
=null;
         
try
         {            
             fos 
=new FileOutputStream(filename);   
             
//  可通過DataHandler類的getInputStream方法讀取上傳資料             writeInputStreamToFile(file.getInputStream(), fos);
             fos.close();
         }
         
catch (Exception e)
         {
             
returnfalse;
         }
         
finally
         {
             
if (fos !=null)
             {
                 
try
                 {
                     fos.close();
                 }
                 
catch (Exception e)
                 {
                 }
             }
         }
         
returntrue;
    }
}

上面程式碼在services.xml檔案的配置程式碼如下:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><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類的物件例項,程式碼如下:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

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

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


<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

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

最後是呼叫uploadWithByte方法,程式碼如下(FileServiceStubwsdl2java生成的stub類名):


<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

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

如果使用C#呼叫FileService,則file引數型別均為byte[],程式碼如下:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->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());
 

其中picUpdateImagec#中載入影象檔案的picturebox控制元件。