title: uploading multiple images to server in android
body: <p>I need to upload multiple images to server at a time in  my android app.i have done the multiple image selection from gallery part and stored the path of selected images in a Arraylist.After successfull uploading ,data is retrived in ajson format as flollows</p>
  
  <pre><code>[{"id":"18","user_id":"136","title":"title","url":[]},{"id":"20","user_id":"136","title":"title","url":[]}]
  </code></pre>
  
  <p>How can i pass the selected images in array format to server..please help</p>
  
  <p>here is my uploding java class</p>
  
  <p><strong>Vis_Upload.java</strong></p>
  
  <pre><code> public class Vis_Upload {
  
  
  String uris[];
      public static final String UPLOAD_URL= "link to upload";
  
      private int serverResponseCode;
  
      public String uploadVideo(ArrayList&lt;String&gt; mArrayUri1, String title, String user_id) {
  uris=new String[mArrayUri1.size()];
          for(int i=0;i&lt;mArrayUri1.size();i++)
          {
              System.out.println("file URI  (in Vis_Upload class)   : "+mArrayUri1.get(i));
              uris[i]=mArrayUri1.get(i).toString();
          }
  
  //replacing 'file' with  'uris[0]'
          String fileName =uris[0].replaceAll(" ","");
  
          System.out.println("filename (in Vis_Upload class)   : "+fileName);
  
          HttpURLConnection conn = null;
          DataOutputStream dos = null;
          String lineEnd = "\r\n";
          String twoHyphens = "--";
          String boundary = "*****";
          int bytesRead, bytesAvailable, bufferSize;
          byte[] buffer;
          int maxBufferSize = 1 * 1024 * 1024;
  
          File sourceFile = new File(uris[0]);
          if (!sourceFile.isFile()) {
              Log.e("Huzza", "Source File Does not exist");
              return null;
          }
          else {
  
              try {
                  FileInputStream fileInputStream = new FileInputStream(sourceFile);
                  URL url = new URL(UPLOAD_URL);
                  conn = (HttpURLConnection) url.openConnection();
                  conn.setDoInput(true);
                  conn.setDoOutput(true);
                  conn.setUseCaches(false);
                  conn.setRequestMethod("POST");
                  conn.setRequestProperty("Connection", "Keep-Alive");
                  conn.setRequestProperty("ENCTYPE", "multipart/form-data");
                  conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                  conn.setRequestProperty("photo", fileName);
                  conn.setRequestProperty("title", title);
                  conn.setRequestProperty("user_id", user_id);
  
                  dos = new DataOutputStream(conn.getOutputStream());
  
  
  
  
                  dos.writeBytes(twoHyphens + boundary + lineEnd);
                  dos.writeBytes("Content-Disposition: form-data; name=\"title\""+ lineEnd);
                  dos.writeBytes(lineEnd);
                  dos.writeBytes(title);
                  dos.writeBytes(lineEnd);
                  dos.writeBytes(twoHyphens + boundary + lineEnd);
  
                  dos.writeBytes("Content-Disposition: form-data; name=\"user_id\""+ lineEnd);
                  dos.writeBytes(lineEnd);
                  dos.writeBytes(user_id);
                  dos.writeBytes(lineEnd);
  
                  dos.writeBytes(twoHyphens + boundary + lineEnd);
                  dos.writeBytes("Content-Disposition: form-data; name=\"photo\";filename=\"" + fileName + "\"" + lineEnd);
                  dos.writeBytes(lineEnd);
  
  
                  bytesAvailable = fileInputStream.available();
                  Log.i("Huzza", "Initial .available : " + bytesAvailable);
  
                  bufferSize = Math.min(bytesAvailable, maxBufferSize);
                  buffer = new byte[bufferSize];
  
                  bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  
                  while (bytesRead &gt; 0) {
                      dos.write(buffer, 0, bufferSize);
                      bytesAvailable = fileInputStream.available();
                      bufferSize = Math.min(bytesAvailable, maxBufferSize);
                      bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                  }
  
                  dos.writeBytes(lineEnd);
                  dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
  
                  serverResponseCode = conn.getResponseCode();
  
                  fileInputStream.close();
                  dos.flush();
                  dos.close();
              } catch (MalformedURLException ex) {
                  ex.printStackTrace();
              } catch (Exception e) {
                  e.printStackTrace();
              }
  
  
              if (serverResponseCode == 200) {
                  StringBuilder sb = new StringBuilder();
                  try {
                      BufferedReader rd = new BufferedReader(new InputStreamReader(conn
                              .getInputStream()));
                      String line;
                      while ((line = rd.readLine()) != null) {
                          sb.append(line);
                      }
                      rd.close();
                  } catch (Exception ioex) {
                      ioex.printStackTrace();
                  }
  
                  System.out.println("(in Vis_upload)sb.toString() : " + sb.toString());
                  return sb.toString();
              } else {
                  return "Could not upload";
              }
          }
      }
  }
  </code></pre>