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