在《WebService之Axis2快速入门(2): 传递复合类型的数据》中讲过,若是要传递二进制文件(如图像、音频文件等),可使用byte[]做为数据类型进行传递,而后客户端使用RPC方式进行调用。这样作只是其中的一种方法,除此以外,在客户端还可使用wsdl2java命令生成相应的stub类来调用WebService,wsdl2java命令的用法详见《WebService之Axis2快速入门(1): 用POJO实现0配置的WebService》。 java
WebService类中包含byte[]类型参数的方法在wsdl2java生成的stub类中对应的数据类型再也不是byte[]类型,而是javax.activation.DataHandler。DataHandler类是专门用来映射WebService二进制类型的。 apache
在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[]了。 c#
下面是一个上传二进制文件的例子,WebService类的代码以下: spa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
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文件的配置代码以下: .net
1
2
3
4
5
6
7
8
9
10
11
12
|
<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类的对象实例,代码以下: orm
1
|
DataHandler dh = new DataHandler(new FileDataSource(imagePath));
|
wsdl2java命令会为每个方法生成一个封装方法参数的类,类名为方法名(第一个字符大写),如uploadWithByte方法生成的类名为UploadWithByte。若是要设置file参数的值,可使用UploadWithByte类的setFile方法,代码以下: xml
1
2
|
UploadWithByte uwb = new UPloadWithByte();
uwb.setFile(dh);
|
最后是调用uploadWithByte方法,代码以下(FileServiceStub为wsdl2java生成的stub类名): 对象
1
2
|
FileServiceStub fss = new FileServiceStub();
fss.uploadWithByte(uwb);
|
若是使用C#调用FileService,则file参数类型均为byte[],代码以下: ip
1
2
3
4
5
6
|
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控件。 ci