在《
WebService大讲堂之Axis2(2):复合类型数据的传递
》中讲过,如果要传递二进制文件(如图像、音频文件等),可以使用
byte[]
作为数据类型进行传递,然后客户端使用
RPC
方式进行调用。这样做只是其中的一种方法,除此之外,在客户端还可以使用
wsdl2java
命令生成相应的
stub
类来调用
WebService
,
命令的用法详见《
WebService大讲堂之Axis2(1):用POJO实现0配置的WebService
》。
WebService 类中包含 类型参数的方法在 生成的 类中对应的数据类型不再是 类型,而是 javax.activation.DataHandler 。 DataHandler 类是专门用来映射 二进制类型的。
在 类中除了可以使用 作为传输二进制的数据类型外,也可以使用 作为数据类型。 不管是使用 ,还是使用 作为 方法的数据类型,使用 命令生成的 类中相应方法的类型都是 。而象使用 .net 、 delphi 类的相应方法类型都是 。这是由于 类是 Java 特有的,对于其他语言和技术来说,并不认识 类,因此,也只有使用最原始的 了。
下面是一个上传二进制文件的例子, 类的代码如下:
FileServiceStub fss FileServiceStub();
fss.uploadWithByte(uwb);
WebService 类中包含 类型参数的方法在 生成的 类中对应的数据类型不再是 类型,而是 javax.activation.DataHandler 。 DataHandler 类是专门用来映射 二进制类型的。
在 类中除了可以使用 作为传输二进制的数据类型外,也可以使用 作为数据类型。 不管是使用 ,还是使用 作为 方法的数据类型,使用 命令生成的 类中相应方法的类型都是 。而象使用 .net 、 delphi 类的相应方法类型都是 。这是由于 类是 Java 特有的,对于其他语言和技术来说,并不认识 类,因此,也只有使用最原始的 了。
下面是一个上传二进制文件的例子, 类的代码如下:
package
service;
import java.io.InputStream;
java.io.OutputStream;
java.io.FileOutputStream;
javax.activation.DataHandler;
public class FileService
{
// 使用byte[]类型参数上传二进制文件
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 != )
{
{
fos.close();
}
(Exception e)
{
}
}
}
true ;
}
private void writeInputStreamToFile(InputStream is, OutputStream os) throws Exception
{
int n 0 [] buffer [ 8192 ];
while ((n is.read(buffer)) > )
{
os.write(buffer, , n);
}
}
使用DataHandler类型参数上传文件 uploadWithDataHandler(DataHandler file, String filename)
{
FileOutputStream fos
{
fos FileOutputStream(filename);
可通过DataHandler类的getInputStream方法读取上传数据 writeInputStreamToFile(file.getInputStream(), fos);
fos.close();
}
;
}
}
import java.io.InputStream;
java.io.OutputStream;
java.io.FileOutputStream;
javax.activation.DataHandler;
public class FileService
{
// 使用byte[]类型参数上传二进制文件
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 != )
{
{
fos.close();
}
(Exception e)
{
}
}
}
true ;
}
private void writeInputStreamToFile(InputStream is, OutputStream os) throws Exception
{
int n 0 [] buffer [ 8192 ];
while ((n is.read(buffer)) > )
{
os.write(buffer, , n);
}
}
使用DataHandler类型参数上传文件 uploadWithDataHandler(DataHandler file, String filename)
{
FileOutputStream fos
{
fos FileOutputStream(filename);
可通过DataHandler类的getInputStream方法读取上传数据 writeInputStreamToFile(file.getInputStream(), fos);
fos.close();
}
;
}
}
<
service
name
="fileService"
>
description
文件服务
</ parameter ="ServiceClass"
service.FileService
parameter
messageReceivers messageReceiver mep ="http://www.w3.org/2004/08/wsdl/in-out"
class ="org.apache.axis2.rpc.receivers.RPcmessageReceiver" /> service
description
文件服务
</ parameter ="ServiceClass"
service.FileService
parameter
messageReceivers messageReceiver mep ="http://www.w3.org/2004/08/wsdl/in-out"
class ="org.apache.axis2.rpc.receivers.RPcmessageReceiver" /> service
DataHandler dh
DataHandler(
FileDataSource(imagePath));
wsdl2java
命令会为每一个方法生成一个封装方法参数的类,类名为方法名(第一个字符大写),如uploadWithByte
方法生成的类名为UploadWithByte
。如果要设置file
参数的值,可以使用UploadWithByte
类的setFile
方法,代码如下:
UploadWithByte uwb
UPloadWithByte();
uwb.setFile(dh);
uwb.setFile(dh);
FileServiceStub fss FileServiceStub();
fss.uploadWithByte(uwb);
MemoryStream ms
MemoryStream();
Bitmap bitmap Bitmap(picUpdateImage.Image);
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
service.fileService fs WSC.service.fileService();
fs.uploadWithDataHandler(ms.ToArray());
fs.uploadWithByte(ms.ToArray());
Bitmap bitmap Bitmap(picUpdateImage.Image);
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
service.fileService fs WSC.service.fileService();
fs.uploadWithDataHandler(ms.ToArray());
fs.uploadWithByte(ms.ToArray());