您的位置:首页 > 其它

axis2客户端编码,RPC,OMElement,上传下载实现,服务端验证

2015-06-17 19:33 561 查看
简单做一下使用axis2作为客户端的情况下一些编码的实现内容。
首先应该部署一套服务以应对客户端调用。我要做的例子不是很麻烦,所以只需要三个接口就可以了,一个接收字符串参数,返回字符串参数。另外两个做上传和下载。一共三个接口,我已经在附件上传了一个类,这个类编译后的class文件放到下载的axis2事例工程的WEB-INF下的pojo文件夹中,pojo文件夹是自己创建的。这样就会发布一个有三个方法的接口。具体关于axis2的发布网络上内容很多,我也会在以后博客中做详细解释。为了方便我也在这里放出代码。

Java代码  


import java.io.FileOutputStream;  

import java.text.SimpleDateFormat;  

import java.util.Date;  

/** 

 * 这个是作为服务端的方法类,不能带有命名空间,方法必须为public 

 * 崔素强 

 * cuisuqiang@163.COM 

 */  

public class MyAxis2Service {     

    /* 

     * 请求的方法,输出接收到的参数,返回时间戳字符串 

     */  

    public String getServiceInit(String name){  

        System.out.println("you name is :" + name);  

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  

        return format.format(new Date());  

    }  

      

    // 下载图像  

    public byte[] dowImageWithByte() {  

        byte[] imageByte = null;  

        try {  

            // 下面的代码调用uploadImageWithByte方法上传图像文件  

            // 打开图像文件,确定图像文件的大小  

            java.io.File file = new java.io.File("c:\\test.jpg");  

            java.io.FileInputStream fis = new java.io.FileInputStream("c:\\test.jpg");  

            // 创建保存要上传的图像文件内容的字节数组  

            imageByte = new byte[(int) file.length()];  

             fis.read(imageByte);  

        } catch (Exception e) {  

            return null;  

        } finally {  

              

        }  

        return imageByte;  

    }  

      

    // 上传图像,imageByte参数表示上传图像文件的字节  

    public boolean uploadImageWithByte(byte[] imageByte, int length) {  

        FileOutputStream fos = null;  

        try {  

            // 将上传的图像保存在D盘的test1.jpg文件中  

            fos = new FileOutputStream("d:\\test1.jpg");  

            // 开始写入图像文件的字节  

            fos.write(imageByte, 0, length);  

            fos.close();  

        } catch (Exception e) {  

            return false;  

        } finally {  

            if (fos != null) {  

                try {  

                    fos.close();  

                } catch (Exception e) {  

  

                }  

            }  

        }  

        return true;  

    }  

}  

 其中要上传和下载的文件是图片,请自己事先准备好。
下面创建一个项目,将所需的JAR包加到工程中,发现有的人说如果不知道是那个包就全加进去,可是axis2的包有几十个,我也尝尽了JAR冲突的痛苦,所以我在这里告诉大家,只需要加我附件里面的几个JAR就可以了。
首先编码调用简单接口的代码,传递的参数和返回的参数都是字符串。发现网络上的例子都是RPC的,为了方便学习,我会给出两种调用方式。首先的网络上通用的方式:

Java代码  


import javax.xml.namespace.QName;  

import org.apache.axis2.addressing.EndpointReference;  

import org.apache.axis2.client.Options;  

import org.apache.axis2.rpc.client.RPCServiceClient;  

/** 

 * RPC axis2调用方式 

 * @author cuisuqiang 

 */  

public class RpcCommonClient {  

    @SuppressWarnings("unchecked")  

    public static void main(String[] args) {  

        try {  

            // 获得客户端  

            RPCServiceClient serviceClient = new RPCServiceClient();  

            // 可以在该对象中设置服务端的验证信息  

            Options options = serviceClient.getOptions();  

            EndpointReference targetEPR = new EndpointReference("http://127.0.0.1:8080/axis2/services/MyAxis2Service");  

            options.setTo(targetEPR);  

            // 在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,也就是<wsdl:definitions>元素的targetNamespace属性值  

            QName opAddEntry = new QName("http://ws.apache.org/axis2","getServiceInit");  

            // 参数,如果有多个,继续往后面增加即可,不用指定参数的名称  

            Object[] opAddEntryArgs = new Object[] {"cuisuqiang" };  

            // 返回参数类型,这个和axis1有点区别  

            // invokeBlocking方法有三个参数,其中第一个参数的类型是QName对象,表示要调用的方法名;  

            // 第二个参数表示要调用的WebService方法的参数值,参数类型为Object[];  

            // 第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[]。  

            // 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}  

            // 如果被调用的WebService方法没有返回值,应使用RPCServiceClient类的invokeRobust方法,  

            // 该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同  

            Class[] classes = new Class[] { String.class };  

            System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]);   

        } catch (Exception e) {  

            e.printStackTrace();  

        }  

    }  

}  

 RPC的调用方式还是非常简洁明了的,但是axis2的调用方式是有很多种实现的。下面的实现方式,使用到了一些底层的东西,不过更加让我们开发人员明了:

Java代码  


import java.util.Iterator;  

import org.apache.axiom.om.OMAbstractFactory;  

import org.apache.axiom.om.OMElement;  

import org.apache.axiom.om.OMFactory;  

import org.apache.axiom.om.OMNamespace;  

import org.apache.axis2.addressing.EndpointReference;  

import org.apache.axis2.client.Options;  

import org.apache.axis2.client.ServiceClient;  

  

/** 

 * Axis2客户端 

 * 崔素强 cuisuqiang@163.com 

 */  

public final class AxisWebServiceClient {  

      

    public static void main(String[] args) {  

        // 直接运行也可以看到效果  

        callWebservice2();  

    }  

      

    /** 

     * 客户端调用service 

     */  

    @SuppressWarnings("unchecked")  

    public static void callWebservice2() {  

        try {  

            // 服务地址,服务地址,命名空间,方法名称,组装参数,返回数据解析,可以自己根据实际情况编码  

            EndpointReference targetEPR = new EndpointReference("http://127.0.0.1:8080/axis2/services/MyAxis2Service");   

            ServiceClient serviceClient = new ServiceClient();  

            Options options = new Options();  

            options.setTo(targetEPR);  

            serviceClient.setOptions(options);  

            OMFactory fac = OMAbstractFactory.getOMFactory();                 

            // 命名空间,有时命名空间不增加没事,不过最好加上,因为有时有事,你懂的  

            OMNamespace omNs = fac.createOMNamespace("http://ws.apache.org/axis2", "");           

            //为SOAP Header构造验证信息,如果你的服务端是没有验证的,那么你不用在Header中增加验证信息  

//          OMElement header = fac.createOMElement("AuthenticationToken", omNs);              

//          OMElement ome_user = fac.createOMElement("Username", omNs);  

//          ome_user.setText("");  

//          header.addChild(ome_user);  

//          OMElement ome_pass = fac.createOMElement("Password", omNs);  

//          ome_pass.setText("");  

//          header.addChild(ome_pass);  

//          serviceClient.addHeader(header);  

            //调用据方法  

            OMElement data = fac.createOMElement("getServiceInit", omNs);             

            // 设置参数,如果参数名称没有设置正确,后果自负  

            OMElement metaData = fac.createOMElement("name", omNs);  

            metaData.setText("cuisuqiang");  

            data.addChild(metaData);              

            //发送请求  

 以上方法调用后返回的是OMElement对象,而传递参数也用到了该对象,具体关于该对象的一些内容,可以查看源码了解。
下面我们来看一下,如何上传一个文件,大家也看到了服务端的地方我文件用的是字节数组,为的是不同业务调用方便和统一。同样,先来看一下RPC调用的方式,这里给出一个方法,大家看代码即可:

Java代码  


/** 

 * 上传 

 */  

@SuppressWarnings("unchecked")  

public static void test2() {  

    try {  

        RPCServiceClient serviceClient = new RPCServiceClient();  

        Options options = serviceClient.getOptions();  

        EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2/services/MyAxis2Service");  

        options.setTo(targetEPR);  

        QName opAddEntry;  

        byte[] buffer;  

        // 下面的代码调用uploadImageWithByte方法上传图像文件  

        // 打开图像文件,确定图像文件的大小  

        java.io.File file = new java.io.File("c:\\test.jpg");  

        java.io.FileInputStream fis = new java.io.FileInputStream("c:\\test.jpg");  

        // 创建保存要上传的图像文件内容的字节数组  

        buffer = new byte[(int) file.length()];  

        // 将图像文件的内容读取buffer数组中  

        int n = fis.read(buffer);  

        Object[] opAddEntryArgs = new Object[] { buffer, n };  

        Class[] classes = new Class[] { Boolean.class };  

        opAddEntry = new QName("http://ws.apache.org/axis2","uploadImageWithByte");  

        fis.close();  

        // 开始上传图像文件,并输出uploadImageWithByte方法的返回传  

        System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]);  

    } catch (Exception e) {  

        e.printStackTrace();  

    }  

}  

 从这里我更感觉到了这种调用方式的简洁性,因为参数的传递非常容易,放到参数的数组中就行了。对比一下,再看一下另一种实现方式:

Java代码  


/** 

 * 上传附件 

 */  

@SuppressWarnings("unchecked")  

public static void test1() {  

    try {  

        // 服务地址,服务地址,命名空间,方法名称,组装参数,返回数据解析,可以自己根据实际情况编码  

        EndpointReference targetEPR = new EndpointReference("http://127.0.0.1:8080/axis2/services/MyAxis2Service");  

        ServiceClient serviceClient = new ServiceClient();  

        Options options = new Options();  

        options.setTo(targetEPR);  

        serviceClient.setOptions(options);  

        OMFactory fac = OMAbstractFactory.getOMFactory();  

        OMNamespace omNs = fac.createOMNamespace("http://ws.apache.org/axis2", "");  

        // 调用据方法  

        OMElement data = fac.createOMElement("uploadImageWithByte", omNs);  

        // 增加附件  

        OMElement fileData = fac.createOMElement("imageByte", omNs);  

        java.io.File file = new java.io.File("c:\\test.jpg");  

        FileDataSource fs = new FileDataSource(file);  

        DataHandler fileHandle = new DataHandler(fs);  

        OMText textData = fac.createOMText(fileHandle, true);  

        fileData.addChild(textData);  

        data.addChild(fileData);  

        // 增加元数据  

        OMElement metaData = fac.createOMElement("length", omNs);  

        metaData.setText(file.length() + "");  

        data.addChild(metaData);  

        // 发送请求  

        OMElement results = serviceClient.sendReceive(data);  

        Iterator it = results.getChildElements();  

        while (it.hasNext()) {  

            OMElement el = (OMElement) it.next();  

            System.out.println("element.getLocalName(): "+ el.getLocalName());  

            System.out.println("element.getText(): " + el.getText());  

        }  

    } catch (Exception e) {  

        e.printStackTrace();  

    }  

}  

 虽然看着是没有第一种好看,不过我也很喜欢,为什么?多写点代码有时候才能显出来我们编码人员的重要性啊,呵呵呵,开玩笑了。
一下再看看下载的方法,首先还是使用第一种方式:

Java代码  


/** 

 * 下载 

 */  

public static void test1() {  

    try {  

        RPCServiceClient serviceClient = new RPCServiceClient();  

        Options options = serviceClient.getOptions();  

        EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2/services/MyAxis2Service");  

        options.setTo(targetEPR);  

        QName opAddEntry;  

        byte[] buffer;  

        opAddEntry = new QName("http://ws.apache.org/axis2","dowImageWithByte");  

        buffer = (byte[]) serviceClient.invokeBlocking(opAddEntry,new Object[] {}, new Class[] { byte[].class })[0];  

        FileOutputStream fos = null;  

        try {  

            fos = new FileOutputStream("d:\\test1.jpg");  

            // 开始写入图像文件的字节  

            fos.write(buffer, 0, buffer.length);  

            fos.close();  

        } catch (Exception e) {  

            e.printStackTrace();  

        } finally {  

            if (fos != null) {  

                try {  

                    fos.close();  

                } catch (Exception e) {  

                    e.printStackTrace();  

                }  

            }  

        }  

    } catch (Exception e) {  

        e.printStackTrace();  

    }  

}  

 同样很简单呀,只要强制转换就可以了。下面继续看一下第二种方式的代码:

Java代码  


/** 

 * 下载 

 */  

@SuppressWarnings("unchecked")  

public static void test2() {  

    try {  

        // 服务地址,服务地址,命名空间,方法名称,组装参数,返回数据解析,可以自己根据实际情况编码  

        EndpointReference targetEPR = new EndpointReference("http://127.0.0.1:8080/axis2/services/MyAxis2Service");  

        ServiceClient serviceClient = new ServiceClient();  

        Options options = new Options();  

        options.setTo(targetEPR);  

        serviceClient.setOptions(options);  

        OMFactory fac = OMAbstractFactory.getOMFactory();  

        OMNamespace omNs = fac.createOMNamespace("http://ws.apache.org/axis2", "");  

        // 调用据方法  

        OMElement data = fac.createOMElement("dowImageWithByte", omNs);  

        // 发送请求  

        OMElement results = serviceClient.sendReceive(data);  

        Iterator it = results.getChildElements();  

        while (it.hasNext()) {  

            OMElement el = (OMElement) it.next();  

            OMText binaryNode = (OMText) el.getFirstOMChild();  

            binaryNode.setOptimize(true); //必须加此句,否则会出现ContentID is null的异常!  

            DataHandler dh = (DataHandler) binaryNode.getDataHandler();  

            FileOutputStream fileOutStream = new FileOutputStream("d:\\test.jpg");  

            InputStream is = dh.getInputStream();  

            fileOutStream.write(IOUtils.getStreamAsByteArray(is));  

        }  

    } catch (Exception e) {  

        e.printStackTrace();  

    }  

}  

 在接收到参数后的解析工作确实有点麻烦。不过,你知道就行了,底层的东西依赖少,出问题也好解决。
不过是不是看着有点晕呀,没关系,每次都会上传工程给大家的。在附件中大家解压后可以直接导入到MyEclipse中查看代码。可以直接main方法运行,也可以跑起来点击首页的按钮来触发后台调用。其中MyAxis2Service.java是要部署的服务端代码,不用关心。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: