title: Check real file size while streaming files with FileItemStream
body: <p>I'm writing an API using Spring + apache commons file upload.</p>
  
  <p><a href="https://commons.apache.org/proper/commons-fileupload/" rel="nofollow noreferrer">https://commons.apache.org/proper/commons-fileupload/</a></p>
  
  <p>There is a problem that I faced. I need to validate a file size. If it's bigger then the one that I configure, user should get an error.
  For now, I implemented the upload without this check and it looks like this:</p>
  
  <pre><code>public ResponseEntity insertFile(@PathVariable Long profileId, HttpServletRequest request) throws Exception {
      ServletFileUpload upload = new ServletFileUpload();
      FileItemIterator uploadItemIterator = upload.getItemIterator(request);
      if (!uploadItemIterator.hasNext()) {
          throw new FileUploadException("FileItemIterator was empty");
      }
      while (uploadItemIterator.hasNext()) {
          FileItemStream fileItemStream = uploadItemIterator.next();
          if (fileItemStream.isFormField()) {
              continue;
          }
         //do stuff
      }
      return new ResponseEntity(HttpStatus.OK);
  }
  </code></pre>
  
  <p>It does exactly what I need. It doesn't require me to get file loaded completely to the memory. I use InputStream that I got  to perform further transfer to another service. Eventually, I don't have file loaded to the memory completely at any point of the time. 
  However, that prevents me from getting the total number of bites that were loaded. </p>
  
  <p>Is there a way to handle such validation without downloading file completely or saving it somewhere? </p>
  
  <p>I tried FileItem, but it does require complete loading of the file. </p>