Test environment: axis2-1.6.1, 6.0.20, jdk1.5
Note: This method is only applicable to scenes with files less than 10M (otherwise there will be memory overflow), upload and download large files should be another way.
1. Create a Java class to be published as WebService.
ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException; Public classBlobservice {/** File Upload service*/ Public BooleanUploadFile (String FileName,byte[] bytes) {FileOutputStream fos=NULL; Try{fos=NewFileOutputStream ("f:\\" +fileName); //writes the data in the byte array bytes to the file output stream in the FOSfos.write (bytes); Fos.flush (); }Catch(Exception e) {e.printstacktrace (); return false; }finally{ Try{fos.close (); } Catch(IOException e) {e.printstacktrace (); } } return true; } /** File Download service*/ Public byte[] DownloadFile () {String filepath= "F:\\head.jpg"; FileInputStream in=NULL; byteBytes[] =NULL; Try{ in=NewFileInputStream (filepath); Bytes=New byte[In.available ()]; //reads Bytes.length bytes of data from input stream in into a byte array bytesin.read (bytes); } Catch(Exception e) {e.printstacktrace (); }finally{ Try{in.close (); } Catch(IOException e) {e.printstacktrace (); } } returnbytes; }}
2. Put the compiled class file of the above Java classes into the Axis2\web-inf\pojo directory.
3, write the client program.
Packageclient;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.util.Date;ImportJavax.xml.namespace.QName;Importorg.apache.axis2.addressing.EndpointReference;Importorg.apache.axis2.client.Options;Importorg.apache.axis2.rpc.client.RPCServiceClient; Public classblobrpcclient{ Public Static voidMain (string[] args)throwsException {rpcserviceclient serviceclient=Newrpcserviceclient (); Options Options=serviceclient.getoptions (); EndpointReference Targetepr=NewEndpointReference ("Http://localhost:8080/axis2/services/BlobService"); Options.setto (TARGETEPR); //================= test File upload ==================================String FilePath= "F:\\head.jpg"; FileInputStream FIS=NewFileInputStream (FilePath); //create a byte array that holds the contents of the image file to be uploaded byte[] buffer =New byte[Fis.available ()]; //reads the data in the input stream FIS into the byte array bufferfis.read (buffer); //Set the entry parameter (1, file name, 2, file byte stream array)object[] Opaddentryargs =Newobject[]{"I am uploading the file. jpg", buffer}; //return value typeClass<?>[] Classes =Newclass<?>[]{Boolean.class }; //Specifies the name of the method to invoke and the namespace of the WSDL fileQName Opaddentry =NewQName ("Http://ws.apache.org/axis2", "UploadFile"); //Close the streamFis.close (); //performing file uploadsSystem.out.println (NewDate () + "File Upload start"); Object returnvalue= Serviceclient.invokeblocking (Opaddentry,opaddentryargs, classes) [0]; System.out.println (NewDate () + "File upload end, return value =" +returnvalue); //================= test File Download ==================================Opaddentry=NewQName ("Http://ws.apache.org/axis2", "DownloadFile"); System.out.println (NewDate () + "File download Start"); byteBytes[] = (byte[]) serviceclient.invokeblocking (Opaddentry,Newobject[]{},Newclass[]{byte[].class}) [0]; FileOutputStream FileOutputStream=NewFileOutputStream ("f:\\ I am the downloaded file. jpg"); //writes the data in the byte array bytes to the output stream FileOutputStreamfileoutputstream.write (bytes); Fileoutputstream.flush (); Fileoutputstream.close (); System.out.println (NewDate () + "File Download Complete"); }}
Article Source: http://huangqiqing123.iteye.com/blog/1454819
WebService file upload Download (byte[] Implementation method)