title: Sending ZIP-archive with multipart/form-data POST in Android App
body: <p>I'm trying to post a ZIP-archive. </p>
  
  <p>I have a website, where a file can be uploaded. The file is simply picked with a file chooser. When the "upload"-button is clicked, the website reloads itself. Before the site reloads, it checks, if there was a file submitted with POST. So if a file was chosen, the file is copied to a folder (including some renaming operations). This is the whole upload process on my website, and it works fine.</p>
  
  <p>I want to use this service in my Android app. I just want to submit the file, so that the PHP code can do the rest. I followed some SO-posts and tutorials, but after a check with fileZilla, I sadly noticed, that there was no file in the directory, where it should be.</p>
  
  <p>I am using an AsyncTask. I have three classes which extend AsyncTask, so I am familiar with that. The AsyncTask-Code seems to work fine, too. The problem is my upload code in the doInBackground-method.</p>
  
  <p>Thats how my code looks like:</p>
  
  <pre><code>protected String doInBackground(String... sUrl)
  {
      HttpURLConnection conn = null;
      DataOutputStream dOut = null;
      DataInputStream dIn = null;
  
      String filename = path;
      String lineEnd = "\r\n";
      String twoHyphens = "--";
      String boundary = "*****";
  
      int bytesRead, bytesAvailable, bufferSize;
      byte[] buffer;
      int maxBufferSize = 1*1024*1024;
      String urlString = Constants.URL_UPLOAD;
  
      try
      {
          // ********************** Client Request
  
          FileInputStream fileIn = new FileInputStream(new File(filename));
  
  
          URL url =  new URL(urlString);
  
          conn = (HttpURLConnection) url.openConnection();
          conn.setDoInput(true);
          conn.setDoOutput(true);
          conn.setUseCaches(false);
          conn.setRequestMethod("POST");
          conn.setRequestProperty("Connection", "Keep-Alive");
          conn.setRequestProperty("Content-Type", "multipart/form-data);boundary="+boundary);
          conn.setRequestProperty("Cookie", Constants.COOKIE_KEY + "=" + Constants.cookie);
  
          dOut = new DataOutputStream(conn.getOutputStream());
          dOut.writeBytes(twoHyphens + boundary + lineEnd);
          dOut.writeBytes("Content-Disposition: form-data; name=\"fileupload\";filename=\"" + path + "\"" + lineEnd);
          dOut.writeBytes(lineEnd);
  
          bytesAvailable = fileIn.available();
          bufferSize = Math.min(bytesAvailable, maxBufferSize);
          buffer = new byte[bufferSize];
          bytesRead = fileIn.read(buffer, 0, bufferSize);
  
          while(bytesRead &gt; 0)
          {
              dOut.write(buffer, 0, bufferSize);
              bytesAvailable = fileIn.available();
              bufferSize = Math.min(bytesAvailable, maxBufferSize);
              bytesRead = fileIn.read(buffer, 0, bufferSize);
          }
  
          dOut.writeBytes(lineEnd);
          dOut.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
  
          fileIn.close();
          dOut.flush();
          dOut.close();
      }
      catch(MalformedURLException ex)
      {
          Log.e("Debug", "error: " + ex.getMessage(), ex);
      }
      catch(IOException ioex)
      {
          Log.e("Debug", "error: " + ioex.getMessage(), ioex);
      }
  
      try
      {
          dIn = new DataInputStream(conn.getInputStream());
          String str;
  
          while((str = dIn.readLine()) != null)
          {
              Log.e("Debug", "Server Response " + str);
              responseData = str;
          }
  
          dIn.close();
      }
      catch(IOException ioex)
      {
          Log.e("Debug", "error: " + ioex.getMessage(), ioex);
      }
      return null;
  }
  </code></pre>
  
  <p>I think, my error is in that line:</p>
  
  <pre><code>dOut.writeBytes("Content-Disposition: form-data; name=\"fileupload\";filename=\"" + path + "\"" + lineEnd);
  </code></pre>
  
  <p>I think that line defines the Http-Header. 
  First, I just used the String for "name" which was used in other Posts. Some research showed me, that I have to use the value of the name-tag of that field of my form, which contains the value, which I want to post. In my case that would be the file chooser, which has the name "fileupload". (Or should I take the name of my form, which is "upload"?)
  Another point, which confuses me is the value of "filename". Should I use the whole path, for example "path/to/android.app.zip" or should I just use the filename: "android.app.zip"?</p>
  
  <p>Additionally I think, that there have to be more errors, because I tried all combinations of name and filename.</p>
  
  <p>I hope someone finds the error(s).</p>
  
  <p>Thanks for your help!</p>