title: How to upload multiple files in android
body: <p>Want to upload all the files inside a specific folder. Saved the list of files in an array from following code</p>
  
  <pre><code>String path = Environment.getExternalStorageDirectory().toString()+"/backup/";      
  File f = new File(path);        
  File file[] = f.listFiles();
  int fileNum = file.length;
  String[] fileName = new String[fileNum]; 
  for (int i=0; i &lt; file.length; i++) {                               
      fileName[i] = file[i].getName();                                
  }
  </code></pre>
  
  <p>I have following code to upload one file. Where do I loop the code from array fileName so that it upload all the files in that array</p>
  
  <pre><code>package com.example.uploadfile;
  
  import java.io.DataOutputStream;
  import java.io.File;
  import java.io.FileInputStream;
  import java.net.HttpURLConnection;
  import java.net.MalformedURLException;
  import java.net.URL;
  
  import android.app.Activity;
  import android.app.ProgressDialog;
  import android.os.Bundle;
  import android.os.Environment;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class MainActivity extends Activity {
      TextView messageText;
      private Button uploadButton;
      String upLoadServerUri = null;
      int serverResponseCode = 0;
  
      final String uploadFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/backup/";
      String uploadFileName = "sand.jpg";
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          upLoadServerUri = "http://www.example.com/UploadToServer.php";
  
          messageText = (TextView) findViewById(R.id.messageText);
          uploadButton = (Button) findViewById(R.id.uploadButton);            
  
          uploadButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View arg0) {
                  new Thread(new Runnable() {
                      public void run() {                     
                          uploadFile(uploadFilePath + "" + uploadFileName);
                      }
                  }).start();
              }
          });
      }
      public int uploadFile(String sourceFileUri) {
          String fileName = sourceFileUri;
  
          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(sourceFileUri);
  
          try {           
              FileInputStream fileInputStream = new FileInputStream(sourceFile);
              URL url = new URL(upLoadServerUri);
  
              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("uploaded_file", fileName);
  
              dos = new DataOutputStream(conn.getOutputStream());
              dos.writeBytes(twoHyphens + boundary + lineEnd);
              dos.writeBytes("Content-Disposition: form-data; name=uploaded_file;filename=" + fileName + "" + lineEnd);
              dos.writeBytes(lineEnd);
  
              bytesAvailable = fileInputStream.available();
              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();
              final String serverResponseMessage = conn.getResponseMessage();
  
              Log.i("uploadFile", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode);
  
              if (serverResponseCode == 200) {
                  runOnUiThread(new Runnable() {
                      public void run() {
                          String msg = "File Upload Completed.\n";
                          msg += "See uploaded file at\n\nhttp://www.example.com/uploads/" + uploadFileName;
  
                          messageText.setText(msg);
                          Toast.makeText(MainActivity.this, "File Upload Complete:"+serverResponseMessage, Toast.LENGTH_SHORT).show();
                      }
                  });
              }           
              fileInputStream.close();
              dos.flush();
              dos.close();
          } catch (Exception e) {         
              e.printStackTrace();
          }       
          return serverResponseCode;
      }
  }
  </code></pre>