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 }
}