title: Java / Spring - zero file length from InputStream
body: <p>I have a REST application with a client built in Angular and a server built with Spring. I'm trying to send a file with an async request, but I receive a 0-length file. It's strange because in the header I can read the exact size of the file (but not its name) but I have no byte stream.</p>
  
  <p>This is the client-side code.</p>
  
  <pre><code>$scope.uploadFile = function() {
      var fd = new FormData()
      for (var i in $scope.files) {
          fd.append("uploadedFile", $scope.files[i])
      }
      var xhr = new XMLHttpRequest()
      xhr.upload.addEventListener("progress", uploadProgress, false)
      xhr.addEventListener("load", uploadComplete, false)
      xhr.addEventListener("error", uploadFailed, false)
      xhr.addEventListener("abort", uploadCanceled, false)
      xhr.open("POST", CONST_SERVER_HOST+"/upload/?token="+$scope.token)
      $scope.progressVisible = true
      xhr.send(fd)
  }
  </code></pre>
  
  <p>This is the server-side code, with all the @RequestMapping signature:</p>
  
  <pre><code>@RequestMapping(value = "/upload/", method = RequestMethod.POST, headers = "content-type=multipart/form-data")
  public String uploadChunked(
          final HttpServletRequest request,
          final HttpServletResponse response) {
      String res;
      System.out.println(request.getHeader("content-range"));
      System.out.println(request.getHeader("content-length"));
      System.out.println(request.getHeader("content-disposition"));
      System.out.println(request.getHeader("content-type"));
      InputStream lettura = null;
      OutputStream scrittura;
      try
      {
          File targetFile = new File("file.pdf");
          OutputStream outStream = new FileOutputStream(targetFile);
          lettura = request.getInputStream();
          byte[] buffer = new byte[1024];
          int len;
          int total = 0;
          while ((len = lettura.read(buffer)) != -1) {
              total += 1024;
              System.out.println("content" + len);
              outStream.write(buffer, 0, len);
          }
          lettura.close();
          outStream.close();
          res = total+" Bytes";
      }
      catch (IOException e)
      {
          e.printStackTrace();
          res = "exception";
      }
      System.out.println("Ret: "+res);
      return res;
  }
  </code></pre>
  
  <p>And, finally, this is the print I get server-side:</p>
  
  <pre><code>null //content-range
  15021 //content-length - perfect!
  null //content-disposition
  multipart/form-data; boundary=----WebKitFormBoundary3F1AuFnuZQqJTRK8 //content-type
  Ret: 0 Bytes // :(
  </code></pre>
  
  <p>Where am I wrong with this code?</p>
  
  <p>Thanks in advance!</p>