WebService complete File upload download

Source: Internet
Author: User
Tags sendfile lenovo wsdl

Since development requires the use of WebService, the first contact tool is called AXIS2. Project development related jar download.

Service side:

Startup class:

Importjava.net.InetAddress;ImportJavax.xml.ws.Endpoint; Public classStartService { Public Static voidMain (string[] args) {Try {            //Get current IPString IP =inetaddress.getlocalhost (). gethostaddress (); //Publish the service to the specified pathSystem.out.println ("IP:" +IP); String Relativelypath=system.getproperty ("User.dir");            System.out.println (Relativelypath); Endpoint.publish ("/http" +ip+ ": 9527/WEBSERVICE/CBRC",Newwebserviceimp ()); System.out.println ("WebService released successfully! "); } Catch(Exception e) {System.out.println ("WebService Publishing failed!" ");        ; }    }}

Implementation class:

ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportJavax.jws.WebService;ImportSun.misc.BASE64Decoder;ImportSun.misc.BASE64Encoder;//import Decoder.base64decoder;//import Decoder.base64encoder;@WebService Public classWebserviceimp { PublicString SendFile (string name,string file)throwsException {//Uploading FilesString Preference_path = "/webserviceupload"; String Relativelypath=system.getproperty ("User.dir"); //Storage PathString FileUpload = Relativelypath +Preference_path; File filepath=NewFile (FileUpload); if(!filepath.exists ())        {Filepath.mkdirs (); }                /*** Generate upload file*/FileOutputStream Fos=NULL; Try{fos=NewFileOutputStream (fileupload + file.separator +name); byte[] Filebs =NewBase64decoder (). Decodebuffer (file);        Fos.write (Filebs); } Catch(Exception e) {e.printstacktrace (); } finally{fos.close (); } System.out.println ("----------------------------------------------------------------------------------------"); System.out.println ("----------------------------------------------------------------------------------------"); System.out.println ("The file" +name+ "was gotten!!"); System.out.println ("----------------------------------------------------------------------------------------"); System.out.println ("----------------------------------------------------------------------------------------"); intSplitindex = Name.lastindexof (".")); String NewName= Name.substring (0,splitindex) + ". pdf"; Transfertopdfutil.pdfmanager (FileUpload+file.separator+ name, FileUpload +file.separator+newName); System.out.println ("----------------------------------------------------------------------------------------"); System.out.println ("Finish File Transfer"); System.out.println ("----------------------------------------------------------------------------------------"); /*** Upload files to client*/File Loc_file=NewFile (FileUpload +file.separator+newName); FileInputStream FIS=NULL; String out=NULL; Try{FIS=NewFileInputStream (Loc_file); byte[] bs =New byte[(int) Loc_file.length ()];            Fis.read (BS); out=NewBase64encoder (). Encode (BS);        Fis.close (); } Catch(FileNotFoundException e) {e.printstacktrace (); } System.out.println ("----------------------------------------------------------------------------------------"); System.out.println ("----------------------------------------------------------------------------------------"); System.out.println ("Return CBRC"); System.out.println ("----------------------------------------------------------------------------------------"); System.out.println ("----------------------------------------------------------------------------------------"); returnOut ; }    }

Client side:
ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJavax.xml.namespace.QName;Importorg.apache.axis2.addressing.EndpointReference;Importorg.apache.axis2.client.Options;Importorg.apache.axis2.rpc.client.RPCServiceClient;ImportSun.misc.BASE64Decoder;ImportSun.misc.BASE64Encoder; Public classCBRC { Public Static voidMain (string[] args)throwsException {//call WebService using RPC modeRpcserviceclient serviceclient =Newrpcserviceclient (); Options Options=serviceclient.getoptions (); EndpointReference Targetepr=NewEndpointReference ("HTTP://10.74.3.191:9527/WEBSERVICE/CBRC?WSDL");//Specifies the URL to call WebServiceOptions.setto (TARGETEPR); //The invokeblocking method of the Rpcserviceclient class calls the method in WebService. The Invokeblocking method has three parameters, where the type of the first parameter is the QName object, which represents the method name to invoke, and the second parameter represents the parameter value of the WebService method to invoke, and the parameter type is object[] The third parameter represents a class object of the return value type of the WebService method, with the parameter type class[]. When a method has no arguments, the second parameter value of the Invokeblocking method cannot be null, but new object[]{} is used.             /*String endpoint = "http://10.74.3.191: 9527/webservice/cbrc?wsdl ";        Here is the WSDL address service service = new service ();        Call Call Service.createcall ();        Call.settargetendpointaddress (new URL (endpoint)); The Setoperationname method Qname the previous parameter to set namespace, and the latter parameter sets the method that you want to access Call.setoperationname (new Qname ("http://wtp/" ," SendFile ")); The Addparameter method is the method for adding elements Call.addparameter ("arg0", Org.apache.axis.encoding.XMLType.XSD_DATE,        Javax.xml.rpc.ParameterMode.IN);        Call.addparameter ("Arg1", org.apache.axis.encoding.xmltype.xsd_string,javax.xml.rpc.parametermode.in); Sets the return value type Call.setreturntype (xmltype.xsd_string); */                 /*** Upload Files*/File XML1=NewFile ("C:\\users\\lenovo\\desktop\\1.doc"); FileInputStream Fis1=NewFileInputStream (XML1); byte[] Bytes1 =New byte[(int) Xml1.length ()];        Fis1.read (BYTES1); //converts a byte array to a Base64 stringString Base64file =NewBase64encoder (). Encode (bytes1);        Fis1.close (); //Access Target Method//string result = (string) call.invoke (new object[]{"1.doc", base64file}); //class object specifying the data type of the method return valueClass[] Classes =NewClass[] {String.class};//class object specifying the data type of the GetGreeting method return valueQName opaddentry=NewQName ("http://wtp/", "sendFile");//Specify the GetGreeting method to invoke and the namespace of the WSDL fileobject[] Opaddentryargs =Newobject[]{"1.doc", base64file};//specifying the parameter value of the GetGreeting methodString result = serviceclient.invokeblocking (Opaddentry, Opaddentryargs, classes) [0].tostring (); /*** Download File*/FileOutputStream Fos=NewFileOutputStream ("C:\\users\\lenovo\\desktop\\1.pdf"); Fos.write (NewBase64decoder (). Decodebuffer (result));                Fos.close (); System.out.println ("End"); }  }

Original quote:

WebService upload and download of files (not automatically generated)

Other references:

Three ways to publish WebService using the Axis2 method

Axis2 jar package and missing jar package error analysis

WebService complete File upload download

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.