The Axis2 of WebService Hall (4): Binary file transfer

Source: Internet
Author: User
Tags stub

In the WebService Axis2 (2): The transfer of composite type data, if you want to pass binary files (such as images, audio files, etc.), you can use byte[] as the data type, and then the client uses RPC to invoke it. This is only one of the methods, in addition, the client can also use the Wsdl2java command to generate the corresponding stub class to invoke the use of the Webservice,wsdl2java command detailed in the WebService Axis2 (1): Pojo to achieve the WebService of the 0 configuration.

The method in the WebService class that contains the byte[] type parameter is no longer a byte[type in the Wsdl2java generated stub class, but rather a javax.activation.DataHandler. The DataHandler class is specifically used to map WebService binary types.

In the WebService class, you can use Javax.activation.DataHandler as a data type in addition to the data types that can be used as transport binaries in byte[. Either using byte[] or using Javax.activation.DataHandler as the data type of the WebService method, the stub class that is generated using the Wsdl2java command The type of the method should be javax.activation.DataHandler. The corresponding method type for the stub class generated by the. NET and Delphi is byte[]. This is because the Javax.activation.DataHandler class is Java-specific and, for other languages and technologies, does not recognize the Javax.activation.DataHandler class, and therefore only uses the most primitive byte[].

The following is an example of uploading a binary file, the code for the WebService class is as follows:

 package service; Import java.io.InputStream; import java.io.OutputStream; import java.io.FileOutputStream; import
Javax.activation.DataHandler;
    public class Fileservice {//Use byte[] type parameter upload binaries 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 (); The catch (Exception e) {}}} return Tru
    E
         } 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);
        
         Use the DataHandler type parameter to upload files public boolean uploadwithdatahandler (datahandler file, String filename) {
         FileOutputStream fos = null;   
             try {fos = new fileoutputstream (filename);
             The upload data can be read by the getInputStream method of the DataHandler class (File.getinputstream (), FOS);
         Fos.close ();
         catch (Exception e) {return false;
                     finally {if (FOS!= null) {try {
                 Fos.close (); The catch (Exception e) {}}} return Tru
    E }
}

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.