WebService大講堂之Axis2(4):二進位制檔案傳輸
阿新 • • 發佈:2019-01-07
在《WebService大講堂之Axis2(2):複合型別資料的傳遞》中講過,如果要傳遞二進位制檔案(如影象、音訊檔案等),可以使用byte[]作為資料型別進行傳遞,然後客戶端使用RPC方式進行呼叫。這樣做只是其中的一種方法,除此之外,在客戶端還可以使用wsdl2java命令生成相應的stub類來呼叫WebService,wsdl2java命令的用法詳見《WebService大講堂之Axis2(1):用POJO實現0配置的WebService》。
WebService類中包含byte[]型別引數的方法在wsdl2java生成的stub類中對應的資料型別不再是byte[]型別,而是
在WebService類中除了可以使用byte[]作為傳輸二進位制的資料型別外,也可以使用javax.activation.DataHandler作為資料型別。不管是使用byte[],還是使用javax.activation.DataHandler作為WebService方法的資料型別,使用wsdl2java命令生成的stub類中相應方法的型別都是javax.activation.DataHandler。而象使用.net、delphi生成的stub類的相應方法型別都是
下面是一個上傳二進位制檔案的例子,WebService類的程式碼如下:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->package service;
import java.io.InputStream;
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方法,程式碼如下(FileServiceStub為wsdl2java生成的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());
其中picUpdateImage為c#中載入影象檔案的picturebox控制元件。