在实际的应用中,不仅需要使用WebService来传递简单类型的数据,有时也需要传递更复杂的数据,这些数据可以被称为复合类型的数据。数组与类(接口)是比较常用的复合类 型。在Axis2中可以直接使用将WebService方 法的参数或返回值类型声明成数组或类(接口)。但要注意,在定义数组类型时只能使用一维数组,如果想传递多维数组,可以使用分隔符进行分隔,如下面的代码 所示:
?
- String[] strArray = new String[]{ "自行车,飞机,火箭","中国,美国,德国","超人,蜘蛛侠,钢铁侠" } ;
?
?
?
复制代码
? ? 上面的代码可以看作是一个3*3的二维数组。 ? ? 在传递类的对象实例时,除了直接将数组类型声明成相应的类或接口,也可以将对象实例进行序列化,也就是说,将 一个对象实例转换成字节数组进行传递,然后接收方再进行反序列化,还原这个对象实例。 ? ? 下面的示例代码演示了如何传递数组与类(接口)类型的数据,并演示如何使用字节数组上传图像。本示例的客户端 代码使用Java和C#编写。要完成这个例子需要如下几步:
一、实现服务端代码
?
? ? ComplexTypeService
是一个WebService
类,该类的代码如下:
?
?
- import java.io.FileOutputStream;
- import data.DataForm;
- ?
- public class ComplexTypeService
- {
- ? ? //??上传图像,imageByte参数表示上传图像文件的字节,
- ? ? //??length参数表示图像文件的字节长度(该参数值可能小于imageByte的数组长度)
- ? ? public boolean uploadImageWithByte(byte[] imageByte,int length)
- ? ? {
- ? ?? ???FileOutputStream fos = null;
- ? ?? ???try
- ? ?? ???{
- ? ?? ?? ?? ?//??将上传的图像保存在D盘的test1.jpg文件中
- ? ?? ?? ?? ?fos = new FileOutputStream("d://test1.jpg");
- ? ?? ?? ?? ?//??开始写入图像文件的字节
- ? ?? ?? ?? ?fos.write(imageByte,length);
- ? ?? ?? ?? ?fos.close();
- ? ?? ???}
- ? ?? ???catch (Exception e)
- ? ?? ???{
- ? ?? ?? ?? ?return false;
- ? ?? ???}
- ? ?? ???finally
- ? ?? ???{
- ? ?? ?? ?? ?if (fos != null)
- ? ?? ?? ?? ?{
- ? ?? ?? ?? ?? ? try
- ? ?? ?? ?? ?? ? {
- ? ?? ?? ?? ?? ?? ???fos.close();
- ? ?? ?? ?? ?? ? }
- ? ?? ?? ?? ?? ? catch (Exception e)
- ? ?? ?? ?? ?? ? {
- ?
- ? ?? ?? ?? ?? ? }
- ? ?? ?? ?? ?}
- ? ?? ???}
- ? ?? ???return true;
- ? ? }
- ? ? //??返回一维字符串数组
- ? ? public String[] getArray()
- ? ? {
- ? ?? ???String[] strArray = new String[]{ "自行车","飞机","火箭" };
- ? ?? ???return strArray;
- ? ? }
- ? ? //??返回二维字符串数组
- ? ? public String[] getMDArray()
- ? ? {
- ? ?? ???String[] strArray = new String[]{ "自行车,钢铁侠" } ;
- ? ?? ???return strArray;
- ? ? }
- ? ? //??返回DataForm类的对象实例
- ? ? public DataForm getDataForm()
- ? ? {
- ? ?? ???return new DataForm();
- ? ? }
- ? ? //??将DataForm类的对象实例序列化,并返回序列化后的字节数组
- ? ? public byte[] getDataFormBytes() throws Exception
- ? ? {
- ? ?? ???java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
- ? ?? ???java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(baos);
- ? ?? ???oos.writeObject(new DataForm());? ?? ???
- ? ?? ???return baos.toByteArray();
- ? ? }? ?
- }
?
?
?
复制代码
二、实现DataForm类
DataForm是要返回的对象实例所对应的类,该类的实现代码如下:
- package data;
- ?
- public class DataForm implements java.io.Serializable
- {
- ? ? private String name = "bill";
- ? ? private int age = 20;
- ?
- ? ? public String getName()
- ? ? {
- ? ?? ???return name;
- ? ? }
- ? ? public void setName(String name)
- ? ? {
- ? ?? ???this.name = name;
- ? ? }
- ? ? public int getAge()
- ? ? {
- ? ?? ???return age;
- ? ? }
- ? ? public void setAge(int age)
- ? ? {
- ? ?? ???this.age = age;
- ? ? }
- }
复制代码
三、发布WebService ? ? 由于本示例的WebService类使用了一个Java类(DataForm类),因此,在发布WebService之前,需要先将DataForm.class文件复制到<Tomcat安装目录>/webapps/axis2/WEB-INF/classes/data目录中,然后将ComplexTypeService.class文件复制到<Tomcat安装目录>/webapps/axis2/WEB-INF/pojo目 录中,最后启动Tomcat(如果Tomcat已经 启动,由于增加了一个DataForm类,因此,需要重新启动Tomcat)。 四、使用Java编 写调用WebService的客户端代码 ? ? 在客户端仍然使用了RPC的调用方式,代码如下:
?
- package client;
- ?
- import javax.xml.namespace.QName;
- import org.apache.axis2.addressing.EndpointReference;
- import org.apache.axis2.client.Options;
- import org.apache.axis2.rpc.client.RPCServiceClient;
- ?
- public class ComplexTypeRPCClient
- {
- ?
- ? ? public static void main(String[] args) throws Exception
- ? ? {
- ? ?? ???RPCServiceClient serviceClient = new RPCServiceClient();
- ? ?? ???Options options = serviceClient.getOptions();
- ? ?? ???EndpointReference targetEPR = new EndpointReference(
- ? ?? ?? ?? ?? ? "http://localhost:8080/axis2/services/ComplexTypeService");
- ? ?? ???options.setTo(targetEPR);
- ? ?? ???// 下面的代码调用uploadImageWithByte方法上传图像文件
- ? ?? ???/////////////////////////////////////////
- ? ?? ???// 打开图像文件,确定图像文件的大小
- ? ?? ???java.io.File file = new java.io.File("f://images.jpg");
- ? ?? ???java.io.FileInputStream fis = new java.io.FileInputStream("f://images.jpg");
- ? ?? ???// 创建保存要上传的图像文件内容的字节数组
- ? ?? ???byte[] buffer = new byte[(int) file.length()];
- ? ?? ???// 将图像文件的内容读取buffer数组中
- ? ?? ???int n = fis.read(buffer);
- ? ?? ???System.out.println("文件长度:" + file.length());
- ? ?? ???Object[] opAddEntryArgs = new Object[]{ buffer,n };
- ? ?? ???Class[] classes = new Class[]{ Boolean.class };
- ? ?? ???QName opAddEntry = new QName("http://ws.apache.org/axis2","uploadImageWithByte");
- ? ?? ???fis.close();
- ? ?? ???// 开始上传图像文件,并输出uploadImageWithByte方法的返回传
- ? ?? ???System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs,classes)[0]);
- ? ?? ???/////////////////////////////////////////
- ? ?? ???
- ? ?? ???// 下面的代码调用了getArray方法,并返回一维String数组
- ? ?? ???/////////////////////////////////////////??
- ? ?? ???opAddEntry = new QName("http://ws.apache.org/axis2","getArray");
- ? ?? ???String[] strArray = (String[]) serviceClient.invokeBlocking(opAddEntry,
- ? ?? ?? ?? ?? ?? ?? ?? ?? ? new Object[]{},new Class[]{String[].class })[0];
- ? ?? ???for (String s : strArray)
- ? ?? ?? ?? ?System.out.print(s + "??");
- ? ?? ???System.out.println();
- ? ?? ???/////////////////////////////////////////
- ? ?? ???
- ?
- ? ?? ???// 下面的代码调用了getMDArray方法,并返回一维String数组
- ? ?? ???/////////////////////////////////////////??
- ? ?? ???opAddEntry = new QName("http://ws.apache.org/axis2","getMDArray");
- ? ?? ???strArray = (String[]) serviceClient.invokeBlocking(opAddEntry,new Object[]{},
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? new Class[]{String[].class})[0];
- ? ?? ???for (String s : strArray)
- ? ?? ???{
- ? ?? ?? ?? ?String[] array = s.split(",");
- ? ?? ?? ?? ?for(String ss: array)
- ? ?? ?? ?? ?? ? System.out.print("<" + ss + "> ");
- ? ?? ?? ?? ?System.out.println();
- ? ?? ???}
- ? ?? ???System.out.println();
- ? ?? ???/////////////////////////////////////////
- ?
- ? ?? ???// 下面的代码调用了getDataForm方法,并返回DataForm对象实例
- ? ?? ???/////////////////////////////////////////??
- ? ?? ???opAddEntry = new QName("http://ws.apache.org/axis2","getDataForm");
- ? ?? ???data.DataForm df = (data.DataForm) serviceClient.invokeBlocking(opAddEntry,
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?new Class[]{data.DataForm.class})[0];
- ? ?? ???System.out.println(df.getAge());
- ? ?? ???/////////////////////////////////////////
- ? ?? ???
- ? ?? ???// 下面的代码调用了getDataFormBytes方法,并返回字节数组,最后将返回的字节数组反序列化后,转换成DataForm对象实例
- ? ?? ???/////////////////////////////////////////? ?? ?? ?
- ? ?? ???opAddEntry = new QName("http://ws.apache.org/axis2","getDataFormBytes");
- ? ?? ???buffer = (byte[]) serviceClient.invokeBlocking(opAddEntry,new Class[]{byte[].class})[0];
- ? ?? ???java.io.ObjectInputStream ois = new java.io.ObjectInputStream(
- ? ?? ?? ?? ?? ? new java.io.ByteArrayInputStream(buffer));
- ? ?? ???df = (data.DataForm) ois.readObject();
- ? ?? ???System.out.println(df.getName());
- ? ?? ???//////////////////////////////////////////
- ? ? }
- }
?
?
?
复制代码
? ? 运行上面的程序,将输出如下的内容:
文件长 度:3617
?
true
自行车 飞机 火箭
<自行车> <飞机> <火箭>
<中国> <美国> <德国>
<超人> <蜘蛛侠> <钢铁侠>
20
bill
?
五、使用C#编写调用WebService的客户端代 码 ? ? 在Visual Studio中使用WebService就简单得多。假设引用WebService时的引用名为complexType,则下面的代码调用了uploadImageWithByte方法来上传图像 文件。在Visual Studio引用WebService时,uploadImageWithByte方法多了两个out参数,在使用时要注意。
?
- complexType.ComplexTypeService cts = new WSC.complexType.ComplexTypeService();
- System.IO.FileStream fs = new System.IO.FileStream(@"f:/images.jpg",System.IO.FileMode.Open);
- byte[] buffer = new byte[fs.Length];
- fs.Read(buffer,(int)fs.Length);
- bool r;
- bool rs;
- cts.uploadImageWithByte( buffer,(int)fs.Length,true,out r,out rs);
?
?
?
复制代码
? ?在获得二维数组时,可以将数据加载到DataGridView或 其他类似的控件中,代码如下:
?
- String[] strArray = cts.getMDArray();
- for (int i = 0; i < strArray.Length; i++)
- {
- ? ? //??用正则表达式将带分隔符的字符串转换成String数组
- ? ? String[] columns = strArray[i].Split(‘,’);
- ? ? //??如果DataGridView的表头不存在,向DataGridView控件添加三个带表头的列
- ? ? if (dataGridView1.Columns.Count == 0)
- ? ?? ???for (int j = 0; j < columns.Length; j++)
- ? ?? ?? ?? ?dataGridView1.Columns.Add("column" + (j + 1).ToString(),"列" + (j + 1).ToString());
- ? ? //??添加行
- ? ? dataGridView1.Rows.Add(1);
- ? ? for(int j = 0; j < columns.Length; j++)
- ? ? {
- ? ?? ???dataGridView1.Rows[i].Cells[j].Value = columns[j];? ?? ?? ?? ?? ?? ?? ???
- ? ? }? ?? ?? ?? ?? ?
- }
?
?
?
复制代码
? ?向DataGridView控件添加数据后的效果如图1所示。
?
?

图1??
?
? ? 对于其他的WebService方法的调用都非常简单,读 者可以自己做这个实验。 ? ? 要注意的是,由于.net和java序列化和反序列化的差异,通过序列化的方式传递对象实例只使用于客户端与服务端为同一种语言或技术的情况,如客户端和服务端都使用Java来编写。? ? 如果读者要上传大文件,应尽量使用FTP的方式来传递,而只通过WebService方法来传递文件名等信息。这样有助于提高传输效率
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|