package s1; ? import java.io.ByteArrayInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.ObjectInputStream; import java.util.Arrays; ? import org.junit.Test; ? import services.ComplexTypeServiceStub; ? import axis2.client.Axis2ClientUtilsForRPC; ? public class ComplexTypeServiceTest { ??? Axis2ClientUtilsForRPC client = new Axis2ClientUtilsForRPC( ?????????? "http://127.0.0.1:8080/as/services/ComplexTypeService", ?????????? "http://services"); ? ??? @Test ??? public void testUploadImageWithByte() { ?????? Object[] opAddEntryArgs; ?????? Class[] classes; ?????? try { ? ?????????? // 打开图像文件,确定图像文件的大小 ?????????? java.io.File file = new java.io.File("f://1.jpg"); ?????????? java.io.FileInputStream fis = new java.io.FileInputStream(file); ?????????? // 创建保存要上传的图像文件内容的字节数组 ?????????? byte[] buffer = new byte[(int) file.length()]; ?????????? // 将图像文件的内容读取buffer数组中 ?????????? int n = fis.read(buffer); ?????????? System.out.println("文件长度:" + file.length()); ?????????? opAddEntryArgs = new Object[] { buffer,n }; ?????????? classes = new Class[] { Boolean.class }; ?????????? fis.close(); ?????????? // 开始上传图像文件,并输出uploadImageWithByte方法的返回传 ?????????? System.out.println(client.invoke("uploadImageWithByte", ????????????????? opAddEntryArgs,classes)[0]); ?????? } catch (FileNotFoundException e) { ? ?????????? e.printStackTrace(); ?????? } catch (IOException e) { ? ?????????? e.printStackTrace(); ?????? } ? ??? } ? ??? @Test ??? public void testGetArray() { ?????? String[] v = (String[]) client.invokeForObject("getArray", ????????????? new Object[] {},new Class[] { String[].class }); ?????? System.out.println(Arrays.asList(v)); ??? } ? ??? @Test ??? public void testGetMDArray() { ?????? String[] v = (String[]) client.invokeForObject("getMDArray",new Class[] { String[].class }); ?????? System.out.println(Arrays.asList(v)); ??? } ? ??? @Test ??? public void testGetDataForm() { ?????? Class[] returnTypes = new Class[] { ComplexTypeServiceStub.DataForm.class }; ?????? // 调用远程服务,得到返回的object数组 ?????? Object[] response = client.invoke("getDataForm",new Object[] {}, ????????????? returnTypes); ?????? // 强制转换成 对象 ?????? ComplexTypeServiceStub.DataForm form = (ComplexTypeServiceStub.DataForm) response[0]; ?????? System.out.println("DataForm: name:" + form.getName() + ",age:" ????????????? + form.getAge()); ??? } ? ??? @Test ??? public void testGetDataFormBytes() { ?????? try { ?????????? byte[] v = (byte[]) client.invokeForObject("getDataFormBytes", ????????????????? new Object[] {},new Class[] { byte[].class }); ? ?????????? ObjectInputStream o = new ObjectInputStream( ????????????????? new ByteArrayInputStream(v)); ?????????? Object object = o.readObject(); ?????????? services.DataForm form1 = (services.DataForm) object; ?????????? System.out.println("DataForm: name:" + form1.getName() + ",age:" ????????????????? + form1.getAge()); ?????? } catch (IOException e) { ?????????? // TODO Auto-generated catch block ?????????? e.printStackTrace(); ?????? } catch (ClassNotFoundException e) { ?????????? // TODO Auto-generated catch block ?????????? e.printStackTrace(); ?????? } ? ??? } ? } ? ? package s1; ? import java.io.FileNotFoundException; import java.io.IOException; import java.io.ObjectInputStream; import java.rmi.RemoteException; import java.util.Arrays; ? import javax.activation.DataHandler; import javax.xml.namespace.QName; ? import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; ? import services.ComplexTypeServiceStub; import services.ExceptionException0; import services.ComplexTypeServiceStub.DataForm; import services.ComplexTypeServiceStub.UploadImageWithByteResponse; ? public class Main { ? ??? /** ??? ?* @param args ??? ?*/ ??? public static void main(String[] args) { ?????? c2(); ??? } ? ??? private static void c1() { ?????? try { ?????????? s1.CustomerServiceStub stub = new s1.CustomerServiceStub( ????????????? ??? "http://127.0.0.1:8080/as/services/CustomerService"); ?????????? s1.CustomerServiceStub.GetCustomer c = new CustomerServiceStub.GetCustomer(); ?????????? c.setCustId(111050l); ? ?????????? System.out.println(stub.getCustomer(c).get_return()); ?????? } catch (AxisFault e) { ?????????? e.printStackTrace(); ?????? } catch (RemoteException e) { ?????????? e.printStackTrace(); ?????? } ??? } ? ??? private static void c2() { ?????? try { ?????????? ComplexTypeServiceStub stub = new ComplexTypeServiceStub(); ?????????? // 调用一维数组 ?????????? ComplexTypeServiceStub.GetArrayResponse res1 = stub.getArray(); ?????????? System.out.println(Arrays.asList(res1.get_return())); ?????????? // 调用二维数组 ?????????? ComplexTypeServiceStub.GetMDArrayResponse res2 = stub.getMDArray(); ?????????? System.out.println(Arrays.asList(res2.get_return())); ?????????? // DataForm类的对象实例 ?????????? ComplexTypeServiceStub.GetDataFormResponse res3 = stub ????????????????? .getDataForm(); ?????????? DataForm form = res3.get_return(); ?????????? System.out.println("DataForm: name:" + form.getName() + ",age:" ????????????????? + form.getAge()); ?????????? // 将DataForm类的对象实例序列化,并返回序列化后的字节数组 ?????????? ComplexTypeServiceStub.GetDataFormBytesResponse res4 = stub ????????????????? .getDataFormBytes(); ?????????? DataHandler dh = res4.get_return(); ?????????? try { ????????????? ObjectInputStream o = new ObjectInputStream(dh.getInputStream()); ????????????? Object object = o.readObject(); ????????????? services.DataForm form1 = (services.DataForm) object; ????????????? System.out.println("DataForm: name:" + form1.getName() ???????????????????? + ",age:" + form1.getAge()); ?????????? } catch (ClassNotFoundException e) { ????????????? e.printStackTrace(); ?????????? } catch (IOException e) { ????????????? e.printStackTrace(); ?????????? } ?????????? // 二进制图像 ?????????? ComplexTypeServiceStub.UploadImageWithByte byte1 = new ComplexTypeServiceStub.UploadImageWithByte(); ?????????? try { ????????????? // 打开图像文件,确定图像文件的大小 ????????????? java.io.File file = new java.io.File("f://1.jpg"); ????????????? java.io.FileInputStream fis = new java.io.FileInputStream(file); ????????????? // 创建保存要上传的图像文件内容的字节数组 ????????????? 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 }; ? ????????????? fis.close(); ? ????????????? UploadImageWithByteResponse res5 = stub ???????????????????? .uploadImageWithByte(byte1); ????????????? res5.get_return(); ?????????? } catch (FileNotFoundException e) { ? ????????????? e.printStackTrace(); ?????????? } catch (IOException e) { ? ????????????? e.printStackTrace(); ?????????? } ? ?????? } catch (AxisFault e) { ? ?????????? e.printStackTrace(); ?????? } catch (RemoteException e) { ? ?????????? e.printStackTrace(); ?????? } catch (ExceptionException0 e) { ?????????? // TODO Auto-generated catch block ?????????? e.printStackTrace(); ?????? } ??? } ? ??? private static void c3() { ?????? try { ?????????? RPCServiceClient client = new RPCServiceClient(); ?????????? Options option = client.getOptions(); ?????????? // 指定客户端访问的webservice服务器端地址 ?????????? EndpointReference erf = new EndpointReference( ????????????? ??? "http://127.0.0.1:8080/as/services/ComplexTypeService"); ?????????? option.setTo(erf); ?????????? // 指定命名空间,指定要调用的方法 ?????????? QName name = new QName("http://services","getDataForm"); ?????????? // 创建返回的参数类型 ?????????? Class[] returnTypes = new Class[] { ComplexTypeServiceStub.DataForm.class }; ?????????? // 调用远程服务,得到返回的object数组 ?????????? Object[] response = client.invokeBlocking(name, ????????????????? returnTypes); ?????????? // 强制转换成 对象 ?????????? ComplexTypeServiceStub.DataForm form = (ComplexTypeServiceStub.DataForm) response[0]; ?????????? System.out.println("DataForm: name:" + form.getName() + ",age:" ??? ????????????? + form.getAge()); ?????? } catch (AxisFault e) { ?????????? // TODO Auto-generated catch block ?????????? e.printStackTrace(); ?????? } ? ??? } ? ??? private static void c4() { ?????? try { ?????????? RPCServiceClient serviceClient = new RPCServiceClient(); ?????????? Options options = serviceClient.getOptions(); ?????????? EndpointReference targetEPR = new EndpointReference( ????????????? ??? "http://127.0.0.1:8080/as/services/ComplexTypeService"); ?????????? options.setTo(targetEPR); ? ?????????? // 下面的代码调用uploadImageWithByte方法上传图像文件 ?????????? // /////////////////////////////////////// ?????????? // 打开图像文件,确定图像文件的大小 ?????????? java.io.File file = new java.io.File("f://1.jpg"); ?????????? java.io.FileInputStream fis = new java.io.FileInputStream(file); ?????????? // 创建保存要上传的图像文件内容的字节数组 ?????????? 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://services", ????????????????? "uploadImageWithByte"); ?????????? fis.close(); ?????????? // 开始上传图像文件,并输出uploadImageWithByte方法的返回传 ?????? ??? System.out.println(serviceClient.invokeBlocking(opAddEntry,classes)[0]); ? ?????? } catch (AxisFault e) { ? ?????????? e.printStackTrace(); ?????? } catch (FileNotFoundException e) { ? ?????????? e.printStackTrace(); ?????? } catch (IOException e) { ? ?????????? e.printStackTrace(); ?????? } ??? } } |