title: File does not upload on server android
body: <p>I tried many methods how load image on server and not one showed any errors. And nothing helped me. Pictures are not uploaded to the server. The file path to image is correct. I also tried to change the file path on the server, it also did not help me and added permission to access files, this did not help.</p>
  
  <p>load file:</p>
  
  <pre><code>public class UploadFiles {
      private String sourceFileUri = "";
  
      public UploadFiles(final String sourceFileUri){
          this.sourceFileUri = sourceFileUri;
          new UploadFileAsync().execute("");
      }
  
      private class UploadFileAsync extends AsyncTask&lt;String, Void, String&gt; {
  
          @Override
          protected String doInBackground(String... params) {
              try {
                  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);
  
                  if (sourceFile.isFile()) {
  
                      try {
                          String upLoadServerUri = "http://myserver/gpstracker/api/v1/users/uploadfile.php";
  
                          // open a URL connection to the Servlet
                          FileInputStream fileInputStream = new FileInputStream(
                                  sourceFile);
                          URL url = new URL(upLoadServerUri);
  
                          // Open a HTTP connection to the URL
                          conn = (HttpURLConnection) url.openConnection();
                          conn.setDoInput(true); // Allow Inputs
                          conn.setDoOutput(true); // Allow Outputs
                          conn.setUseCaches(false); // Don't use a Cached Copy
                          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("bill", sourceFileUri);
  
                          dos = new DataOutputStream(conn.getOutputStream());
  
                          dos.writeBytes(twoHyphens + boundary + lineEnd);
                          dos.writeBytes("Content-Disposition: form-data; name=\"bill\";filename=\""
                                  + sourceFileUri + "\"" + 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);
  
                          }
  
                          // send multipart form data necesssary after file
                          // data...
                          dos.writeBytes(lineEnd);
                          dos.writeBytes(twoHyphens + boundary + twoHyphens
                                  + lineEnd);
  
                          int serverResponseCode = conn.getResponseCode();
  
                          String serverResponseMessage = conn
                                  .getResponseMessage();
                          System.out.println("SERVER: " + serverResponseMessage);
                          if (serverResponseCode == 200) {
                          }
  
                          fileInputStream.close();
                          dos.flush();
                          dos.close();
  
                      } catch (Exception e) {
  
                          e.printStackTrace();
  
                      }
                  } // End else block
  
  
              } catch (Exception ex) {
                  ex.printStackTrace();
              }
              return "Executed";
          }
      }
  
  }
  </code></pre>
  
  <p>php file</p>
  
  <pre><code>&lt;?php
  
   if (is_uploaded_file($_FILES['bill']['tmp_name'])) {
      $uploads_dir = '/var/www/html/gpstracker/api/v1/users/images';
      $tmp_name = $_FILES['bill']['tmp_name'];
      $pic_name = $_FILES['bill']['name'];
      move_uploaded_file($tmp_name, $uploads_dir.$pic_name);
    }
      else{
       echo "File not uploaded successfully.";
    }  
  ?&gt;
  </code></pre>
  
  <p>Server response:</p>
  
  <blockquote>
    <p>System.out: SERVER: OK</p>
  </blockquote>