Axis2 (4): Binary File Transfer

Source: Internet
Author: User

In 《 Axis2 (2) in the WebService Lecture Hall: transfer of composite data As mentioned in, you can use Byte [] Transmits data as the data type, and then the client uses RPC Method. This is only one of the methods. In addition, it can be used on the client. Wsdl2java Command to generate the corresponding Stub Class to call WebService , Wsdl2java For command usage, see 《 Axis2 (1) in the WebService Lecture Hall: Use pojo to implement a WebService with Zero Configuration .
WebService Class contains Byte []The type parameter method is Wsdl2java Generated Stub The corresponding data type in the class is no longer Byte [] Type, Javax. Activation. datahandler . Datahandler Classes are specifically used for ing WebService Binary type.
In WebService In addition Byte [] You can also use Javax. Activation. datahandler As the data type. Whether it is used Byte [] , Or use Javax. Activation. datahandler As WebService The data type of the method. Wsdl2java Command generation Stub The type of the corresponding method in the class is Javax. Activation. datahandler . For example . Net , Delphi Generated Stub The corresponding method types of the class are Byte [] . This is because Javax. Activation. datahandler Class is Java Not familiar with other languages and technologies. Javax. Activation. datahandlerClass. Therefore, only the original Byte [] .
The following is an example of uploading a binary file, WebService ClassCodeAs follows:

Package Service;

ImportJava. Io. inputstream;
ImportJava. Io. outputstream;
ImportJava. Io. fileoutputstream;
ImportJavax. Activation. datahandler;

Public   Class Fileservice
{
// Use byte [] type parameters to upload binary files
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 ();
}
Catch (Exception E)
{
}
}
}
Return   True ;
}
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 datahandler type parameters to upload files
Public   Boolean Uploadwithdatahandler (datahandler file, string filename)
{

Fileoutputstream fos =   Null ;
Try
{
Fos =   New Fileoutputstream (filename );
// You can use the getinputstream method of the datahandler class to read uploaded data.
Writeinputstreamtofile (file. getinputstream (), FOS );
FOS. Close ();
}
Catch (Exception E)
{
Return   False ;
}
Finally
{
If (FOS ! =   Null )
{
Try
{
FOS. Close ();
}
Catch (Exception E)
{
}
}
}
Return   True ;
}
}

The above code is inServices. xmlThe file configuration code is as follows:

< Service Name = "Fileservice" >
< Description >
File Service
</ Description >
< Parameter Name = "Serviceclass" >
Service. fileservice
</ Parameter >
< Messagereceivers >
< Messagereceiver MEP = "Http://www.w3.org/2004/08/wsdl/in-out"
Class = "Org. Apache. axis2.rpc. Receivers. rpcmessagereceiver"   />
</ Messagereceivers >
</ Service >


If you useWsdl2javaCommand generation callJavaClient code, you need to createDatahandlerClass object instance, the Code is as follows:

Datahandler DH =   New Datahandler ( New Filedatasource (ImagePath ));

Wsdl2javaThe command generates a class that encapsulates method parameters for each method. The class name is the method name (the first character is capitalized), as shown in figureUploadwithbyteThe class name generated by the method isUploadwithbyte. If you want to setFileParameter value, which can be usedUploadwithbyteClassSetfileThe Code is as follows:

Uploadwithbyte UWB= NewUploadwithbyte ();
UWB. setfile (DH );

Finally, callUploadwithbyteThe Code is as follows (FileservicestubIsWsdl2javaGeneratedStubClass Name ):

Fileservicestub FSS= NewFileservicestub ();
FSS. uploadwithbyte (UWB );

If you useC #CallFileservice, ThenFileThe parameter types areByte []The Code is as follows:

Memorystream MS =   New Memorystream ();
Bitmap bitmap =   New Bitmap (picupdateimage. Image );
Bitmap. Save (MS, system. Drawing. imaging. imageformat. JPEG );
Service. fileservice FS =   New WSC. Service. fileservice ();
FS. uploadwithdatahandler (Ms. toarray ());
FS. uploadwithbyte (Ms. toarray ());

picupdateimage C # control that loads image files.

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.