title: Save binary file from html form data with sun.net.httpserver
body: <p>I am using sun.net.httpserver from JDK to create a simple http service for my device.</p>
  
  <p>Now I want to save the post form data in to a file, but I always found there are additional content saved in the file.</p>
  
  <pre><code>root@android:/sdcard # cat a.jpg
  -----------------------------288271103222787
  Content-Disposition: form-data; name="myfile"; filename="a.jpg"
  Content-Type: text/plain
  
  CONTENT of the uploaded file
  
  -----------------------------288271103222787--
  </code></pre>
  
  <p>how can I handle this to only save the "CONTENT of the uploaded file"?</p>
  
  <p>My current code is:</p>
  
  <pre><code>private String handleWebSvcApi(HttpExchange exchange, String apiName) throws Exception {
          Log.d(TAG, "handleWebSvcApi : " + apiName);
          InputStream is = exchange.getRequestBody();
          String ret = mWebServiceApiHandler.handleWebServiceApi(apiName, is);
          return ret;
      }
  
  public String handleWebServiceApi(String apiName, InputStream apiData) throws Exception {
          Object ret = null;
          if(apiName.equals("upload")) {
              String filePath = saveApiDataAsFile(apiData);
          }
  }
  
  private static String saveApiDataAsFile(InputStream is) throws IOException {
          BufferedInputStream in = new BufferedInputStream(is);
          File outPutFile = new File(Environment.getExternalStorageDirectory().getPath() + "/newfirmware.zip");
          OutputStream os = new FileOutputStream(outPutFile);
          int len = 0;
          byte buff[] = new byte[8192];
          while ((len = in.read(buff)) &gt; 0) {
              os.write(buff, 0, len);
          }
          os.close();
          return outPutFile.getAbsolutePath();
     }
  </code></pre>