title: Setting file size restrictions when uploading files with Jersey
body: <p>I'm currently implementing functionality for uploading files using jersey rest. I would like to set a maximum allowed file size which to me seems like a pretty common requirement. </p>
  
  <p>My first approach was to use Jerseys FormDataContentDisposition which should hold all the information I could possibly need about the file. But all information except the file name seems to be missing, including file size.</p>
  
  <p>This is my rest method:</p>
  
  <pre><code>@POST
  @Path("uploadFile/")
  @Consumes("multipart/form-data")
  @Produces("text/html")
  public String handleDocumentUpload(
      @FormDataParam("file") InputStream uploadedInputStream,
      @FormDataParam("file") FormDataContentDisposition fileDetail)
  {
      if(fileDetail.getSize() &gt; MAX_FILE_SIZE)
      {
          throw new IllegalArgumentException(
                  "File is to big! Max size is " + MAX_FILE_SIZE);
      }
      // ...more file handling logic
  }
  </code></pre>
  
  <p>Which isn't working since the returned size is always "-1"!</p>
  
  <p>I use a extremely simple html form for the file upload:</p>
  
  <pre><code>&lt;html&gt;
    &lt;head&gt;
      &lt;title&gt;File upload&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
   &lt;p&gt;Choose file&lt;/p&gt;
   &lt;form enctype="multipart/form-data" method="POST" action="uploadFile"&gt;
     &lt;input type="file" name="file" size="50"&gt;
     &lt;input type="submit" value="Upload"&gt;
   &lt;/form&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>So now to my question; how would you enforce a file size restriction using jersey? There must be some simple way without having to resort to reading the whole file into memory (ByteArray) and then get the actuall size, right? </p>