Android Call WebService implementation of image upload

Source: Internet
Author: User

Ext.: http://www.cnblogs.com/top5/archive/2012/02/16/2354517.html

 Public voidtestupload () {Try{String Srcurl= "/sdcard/";//PathString fileName = "Aa.jpg";//file nameFileInputStream FIS =NewFileInputStream (Srcurl +fileName); Bytearrayoutputstream BAOs=NewBytearrayoutputstream (); byte[] buffer =New byte[1024]; intCount = 0;  while((count = fis.read (buffer)) >= 0) {baos.write (buffer,0, Count); } String Uploadbuffer=NewString (Base64.encode (Baos.tobytearray ()));//for BASE64 encodingString methodName = "Uploadimage";   Connectwebservice (Methodname,filename, Uploadbuffer); //Call WebServiceLOG.I ("Connectwebservice", "Start");          Fis.close (); }Catch(Exception e) {e.printstacktrace (); }} Connectwebservice () method://call WebService using KSOAP2    Private Booleanconnectwebservice (String methodname,string fileName, String imagebuffer) {string namespace= "Http://134.192.44.105:8080/SSH2/service/IService";//namespace, which is the server-side interface, note: The suffix does not add. wsdl,//server side I was using X-fire to implement the WebService interfaceString url = "Http://134.192.44.105:8080/SSH2/service/IService";//the corresponding URL//here is the call process, if you do not understand, please see the relevant WebService documentationSoapobject Soapobject =NewSoapobject (namespace, methodName); Soapobject.addproperty ("filename", filename);//parameter 1 Picture nameSoapobject.addproperty ("image", imagebuffer);//parameter 2 picture stringSoapserializationenvelope envelope =NewSoapserializationenvelope (SOAPENVELOPE.VER10); Envelope.dotnet=false;          Envelope.setoutputsoapobject (Soapobject); Httptransportse httptranstation=Newhttptransportse (URL); Try{Httptranstation.call (namespace, envelope); Object result=Envelope.getresponse (); LOG.I ("Connectwebservice", result.tostring ()); } Catch(Exception e) {e.printstacktrace (); }          return false; }  

2. Server-side WebService code:

 Publicstring Uploadimage (string filename, string image) {FileOutputStream fos=NULL; Try{String Todir= "C:\\Program Files\\tomcat 6.0\\webapps\\ssh2\\images";//Storage Path        byte[] buffer =NewBase64decoder (). Decodebuffer (image);//decode the image string that was passed to Android.File DestDir =NewFile (Todir); if(!destdir.exists ()) Destdir.mkdir (); FOS=NewFileOutputStream (NewFile (Destdir,filename));//Save Picturefos.write (buffer);          Fos.flush ();          Fos.close (); return"Upload picture is successful!" + "Picture path:" +Todir; }Catch(Exception e) {e.printstacktrace (); }      return"Upload picture Failed!" "; }  

Unit Test calls the Testupload () method on the Android side, and if you see the green bar, it means the call was successful! Under the server, you can see the pictures you uploaded .... Of course, this demo is very primitive, there is no beautiful UI or anything, but this is the Android side call WebService to upload pictures process. Download from the server to the Android side, and the truth. Welcome everyone to exchange study ....

Android Call WebService implementation of image upload

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.