title: How to upload File to server from android?
body: <p>I have a problem with uploading file to the server.
  Here i'm trying to create the registration form.</p>
  
  <p>I need to upload <strong>all values that taken from user, along with that i need to upload the resume</strong> resume is in PDF format.</p>
  
  <p>Here is my code. Please look into it.</p>
  
  <pre><code>......
  ......
  //here is the connection part
  URL url = new URL(mUrl);
  connection = (HttpURLConnection) url.openConnection();
  connection.setDoInput(true);
  connection.setDoOutput(true);
  connection.setUseCaches(false); // Don't use a Cached Copy
  connection.setRequestMethod("POST");
  connection.setRequestProperty("Connection", "Keep-Alive");
  connection.setRequestProperty("ENCTYPE", "multipart/form-data");
  connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
  connection.setRequestProperty("bill", filePath);
  connection.connect();
  .....
  .....
  
  private int sendFileToServer(String filePath, HttpURLConnection conn, DataOutputStream dataOutputStream, String data) throws IOException {
      String lineEnd = "\r\n";
      String twoHyphens = "--";
      String boundary = "*****";
      int bytesRead, bytesAvailable, bufferSize;
      byte[] buffer;
      int maxBufferSize = 1024 * 1024;
  
      FileInputStream fis = new FileInputStream(filePath);
  
  
      BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
      writer.write(data);
      writer.flush();
  
      dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
      dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"bill\";filename=\""
              + filePath + "\"" + lineEnd);
      dataOutputStream.writeBytes(lineEnd);
  
      // create a buffer of maximum size
      bytesAvailable = fis.available();
  
      bufferSize = Math.min(bytesAvailable, maxBufferSize);
      buffer = new byte[bufferSize];
  
      // read file and write it into form...
      bytesRead = fis.read(buffer, 0, bufferSize);
  
      while (bytesRead &gt; 0) {
          dataOutputStream.write(buffer, 0, bufferSize);
          bytesAvailable = fis.available();
          bufferSize = Math.min(bytesAvailable, maxBufferSize);
          bytesRead = fis.read(buffer, 0,
                  bufferSize);
  
      }
      dataOutputStream.writeBytes(lineEnd);
      dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
      Log.e(TAG, "setFileToServer: res code " + conn.getResponseCode() + " " + conn.getResponseMessage());
      return conn.getResponseCode();
  }
  </code></pre>
  
  <p>I want to know, is there is a way to upload the file along with set of string(user data) to server or we need to separate the file uploading and string(user data).</p>