Android calls WebService to upload images

Source: Internet
Author: User
Tags encode string
Recently, the boss requested to upload and download images from the android client by calling the WebService interface of the server to upload images from Android to the server, and then download images from the server to the android client. After the demand came down, I began to use my brains. Generally, we call WebService, that is, communication between the server and the client (browser, Android mobile phone, etc.). The communication is generally a string in XML or JSON format. Yes, it can only be a string. My idea is to read the image to be uploaded from the android end using an IO stream, encode it with base64 into a character-Throttling string, and call WebService to transmit the string as a parameter to the server end, the server decodes the string and saves it to the corresponding path. The key to the entire upload process is to transmit data using byte stream strings. The download process is opposite to the upload process. Code Change accordingly. There are so many code. The process is: upload an image on Android sdcard to the images folder under the server. Note: This is just a demo. There is no UI interface, and the file path and file name are all written to death. During the runtime, you just need to change it accordingly. 1. Read images from Android sdcard. Public void testupload () {try {string srcurl = "/sdcard/"; // path string filename = "aa.jpg "; // file name: fileinputstream FD = new fileinputstream (srcurl + filename); bytearrayoutputstream baos = new bytearrayoutputstream (); byte [] buffer = new byte [1024]; int COUNT = 0; while (COUNT = FCM. read (buffer)> = 0) {baos. write (buffer, 0, count);} string uploadbuffer = new string (base64.encode (baos. tobytearray (); // Encode string methodname = "uploadimage" by base64; connectwebservice (methodname, filename, uploadbuffer); // call WebService log. I ("connectwebservice", "Start"); FCM. close ();} catch (exception e) {e. printstacktrace () ;}} connectwebservice () method: // call WebService private Boolean connectwebservice (string methodname, string filename, string imagebuffer) {string namespace = "http: // 80 80/SSH2/service/iservice "; // namespace, that is, the server interface. Note: The suffix is not added. WSDL, // The string URL of the WebService interface implemented by X-fire on the server side = "http: // 8080/SSH2/service/iservice "; // The corresponding URL // The following is the call process. If you do not understand it, see the relevant WebService documentation soapobject = new soapobject (namespace, methodname); soapobject. addproperty ("FILENAME", filename); // parameter 1 image name soapobject. addproperty ("image", imagebuffer); // parameter 2 image string soapserializationenvelop E envelope = new soapserializationenvelope (soapenvelope. ver10); envelope. DOTNET = false; envelope. setoutputsoapobject (soapobject); httptransportse httptranstation = new httptransportse (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: Public String uploadimage (string filename, string image) {fileoutputstream Fos = NULL; try {string todir = "C: \ Program Files \ Tomcat 6.0 \ webapps \ SSH2 \ images "; // storage path byte [] buffer = new base64decoder (). decodebuffer (image); // Decode File destdir = new file (todir); If (! Destdir. exists () destdir. mkdir (); Fos = new fileoutputstream (new file (destdir, filename); // Save the image FOS. write (buffer); FOS. flush (); FOS. close (); Return "image uploaded successfully! "+" Image path: "+ todir;} catch (exception e) {e. printstacktrace ();} return" An error occurred while uploading the image! ";} The testupload () method is called for unit tests on Android. If you see green bars, the call is successful! On the server, you can see the picture you uploaded .... Of course, this demo is very simple, and there is no beautiful ui or something, but this is the process for Android to call WebService to upload images. The same applies to downloading data from the server to Android. Welcome to exchange and learn ....
Related Article

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: 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.