Android upload multiple pictures to the specified server through webservice
When you browse this, I believe you have a certain understanding of the basic application of webservice. If you don’t understand it, you can read the article I wrote earlier.Click to open the link, I have been developing an app these days, and I need to upload images to the designated server, but I thought there should be such a good article on the Internet, but I found a lot of codes on the Internet. In the process of passing pictures, I encountered various bugs. It is really a Chinese thing that likes to copy other people's, so I don't check the correctness of the code myself. Oh, I was speechless, and then I decided to take some time to fill this void and wrote this blog post so that my friends will not take detours. First, I modified the error code on the Internet. The Android side code is as follows: This is in a method of the asynchronous class, doInBackground(String... params)
InputStream fis; try { fis = new FileInputStream(params[0]+params[1]);//params[0] is the file path, and params[1] is the file name ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; int count =-1; String uploadBuffer = null ; try { while((count = (buffer))!=-1){ (buffer, 0, count); uploadBuffer=((), ); //This cannot be replaced with count for Base64 encoding } SoapObject soapObject=(params[1],uploadBuffer,"getImageFromAndroid",); reque=(0).toString(); } catch (IOException e) { (); } } catch (FileNotFoundException e1) { (); }
Server code
public String getImageFromAndroid(String filename, String image){ OutputStream fos = null; try{ Date date=new Date(); DateFormat format=new SimpleDateFormat("yyyy-MM-dd"); String time=(date); String toDir = "d:\\Program Files\\images\\"+time+"\\"; //Storage pathbyte[] buffer = new BASE64Decoder().decodeBuffer(image); //Decode the image string passed from AndroidFile destDir = new File(toDir); if(!()) (); fos = new FileOutputStream(new File(toDir,filename)); //Save the picture(buffer); (); (); return "The upload of the picture was successful!" + "The image path is:" + toDir; }catch (Exception e){ (); } return "Uploading the image failed!";
In this way, you can upload the image by calling the server method.
However, do you think it's over? ? ? No, no! This can only be uploading one picture at a time, but sometimes we need to upload batches of pictures, what should we do? Since all the pictures are done, let alone the more pictures are added, it will be achieved with a loop. However, it is all because of our youth and frivolity. Things are often not simple. Please read this article:https:///article/
Thank you for reading, I hope it can help you. Thank you for your support for this site!