title: Upload a file through an HTTP form, via MultipartEntityBuilder, with a progress bar
body: <p>The <strong>short version</strong> - <code>org.apache...MultipartEntity</code> is deprecated, and its upgrade, <code>MultipartEntityBuilder</code>, appears under-represented in our online forums. Let's fix that. How does one register a callback, so my (Android) app can display a progress bar as it uploads a file?</p>
  
  <p>The <strong>long version</strong> - Here's the "missing dirt-simple example" of <code>MultipartEntityBuilder</code>:</p>
  
  <pre><code>public static void postFile(String fileName) throws Exception {
      // Based on: https://stackoverflow.com/questions/2017414/post-multipart-request-with-android-sdk
  
      HttpClient client = new DefaultHttpClient();
      HttpPost post = new HttpPost(SERVER + "uploadFile");
      MultipartEntityBuilder builder = MultipartEntityBuilder.create();        
      builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
      builder.addPart("file", new FileBody(new File(fileName)));
      builder.addTextBody("userName", userName);
      builder.addTextBody("password", password);
      builder.addTextBody("macAddress", macAddress);
      post.setEntity(builder.build());
      HttpResponse response = client.execute(post);
      HttpEntity entity = response.getEntity();
  
      // response.getStatusLine();  // CONSIDER  Detect server complaints
  
      entity.consumeContent();
      client.getConnectionManager().shutdown(); 
  
  }  // FIXME  Hook up a progress bar!
  </code></pre>
  
  <p>We need to fix that <code>FIXME</code>. (An added benefit would be interruptible uploads.) But (please correct me whether or not I'm wrong), all the online examples seem to fall short.</p>
  
  <p>This one, <a href="http://pastebin.com/M0uNZ6SB" rel="nofollow noreferrer">http://pastebin.com/M0uNZ6SB</a>, for example, uploads a file as a "binary/octet-stream"; not a "multipart/form-data". I require real fields.</p>
  
  <p>This example, <a href="https://stackoverflow.com/questions/254719/file-upload-with-java-with-progress-bar">File Upload with Java (with progress bar)</a>, shows how to override the <code>*Entity</code> or the <code>*Stream</code>. So maybe I can tell the <code>MultipartEntityBuilder</code> to <code>.create()</code> an overridden entity that meters its upload progress?</p>
  
  <p>So if I want to override something, and replace the built-in stream with a counting stream that sends a signal for every 1000 bytes, maybe I can extend the <code>FileBody</code> part, and override its <code>getInputStream</code> and/or <code>writeTo</code>.</p>
  
  <p>But when I try <code>class ProgressiveFileBody extends FileBody {...}</code>, I get the infamous <code>java.lang.NoClassDefFoundError</code>.</p>
  
  <p>So while I go spelunking around my <code>.jar</code> files, looking for the missing Def, can someone check my math, and maybe point out a simpler fix I have overlooked?</p>