WebService Axis2 (2): The transfer of compound type data

Source: Internet
Author: User

In practical applications, not only do you need to use WebService to pass simple types of data, but sometimes you need to pass more complex data, which can be called composite types of data. Arrays and classes (interfaces) are more commonly used as composite types. In Axis2, you can declare an array or class (interface) directly using the parameter or return value type of the WebService method. Note, however, that only one-dimensional arrays are used when defining an array type, and if you want to pass a multidimensional array, you can use delimiters to delimit them, as shown in the following code:

string[] Strarray = new string[]{"Bicycles, airplanes, rockets", "China, USA, Germany", "Superman, Spider-Man, Iron Man"};

The above code can be seen as a two-dimensional array of 3*3.

When passing an object instance of a class, in addition to declaring the array type directly as the corresponding class or interface, you can also serialize the object instance, that is, convert an object instance into a byte array for delivery, and then deserialize the receiver to restore the object instance.

The following example code demonstrates how to pass the data of an array and Class (interface) type, and shows how to upload an image using a byte array. The client code for this example is written in Java and C #. The following steps are required to complete this example:

First, implement the service-side code

Complextypeservice is a WebService class that has the following code:

 import java.io.FileOutputStream; Import data.
DataForm; 
    public class Complextypeservice {//upload image, Imagebyte parameter indicates the byte of the upload image file,//length parameter indicates the byte length of the image file (this parameter value may be less than the Imagebyte array length)
        public boolean uploadimagewithbyte (byte[] imagebyte, int length) {FileOutputStream FOS = null;
            try {//To save the uploaded image in the D-disk test1.jpg file Fos = new FileOutputStream ("d:\\test1.jpg");
            Bytes Fos.write (imagebyte, 0, length) to begin writing to the image file;
        Fos.close ();
        catch (Exception e) {return false;
                    finally {if (FOS!= null) {try {
                Fos.close ();
    The catch (Exception e) {}}} is return true;
   }//Return one-dimensional string array public string[] GetArray () {string[] Strarray = new string[]{"Bike", "Airplane", "Rocket"};     return strarray; }//Return two-dimensional string array public string[] Getmdarray () {string[] Strarray = new string[]{"Bicycles, airplanes, rockets", "China, USA, Germany"
        "Superman, Spider-Man, Iron Man"};
    return strarray;
    //Returns the object instance public DataForm Getdataform () of the DataForm class () {return new dataform (); ///Serializes the object instance of the DataForm class and returns the serialized byte array public byte[] Getdataformbytes () throws Exception {Java.io.Byte
        Arrayoutputstream BAOs = new Java.io.ByteArrayOutputStream ();
        Java.io.ObjectOutputStream Oos = new Java.io.ObjectOutputStream (BAOs);
        Oos.writeobject (New DataForm ());
    return Baos.tobytearray (); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.