title: How can I read the content of the content-disposition header?
body: <p><strong>TEMPORARY SOLVED:</strong> <a href="https://stackoverflow.com/questions/7455422/inputstream-closed-in-apache-fileupload-api">InputStream closed in Apache FileUpload API</a></p>
  
  <hr>
  
  <p>I want to read the content of the content-disposition header but <code>request.getHeader ("content-disposition")</code> always return null and <code>request.getHeader ("content-type")</code> only returns the first line, like this <code>multipart/form-data; boundary=AaB03x</code>.</p>
  
  <p>Supose I receive the following header:</p>
  
  <pre><code>Content-Type: multipart/form-data; boundary=AaB03x
  
  --AaB03x
  Content-Disposition: form-data; name="submit-name"
  
  Larry
  --AaB03x
  Content-Disposition: form-data; name="files"; filename="file1.txt"
  Content-Type: text/plain
  
  ... contents of file1.txt ...
  --AaB03x--
  </code></pre>
  
  <p>I want to read all the content-disposition headers. How?</p>
  
  <p>Thanks.</p>
  
  <hr>
  
  <p><strong>EDIT1</strong>: What I really want to solve is the problem when the client sends a file that exceeds the maximum size because when you call request.getPart ("something") it doesn't matter what part name you pass to it because it always will throw an IllegalStateException even if the request does not contain this parameter name.</p>
  
  <p>Example:</p>
  
  <pre><code>Part part = request.getPart ("param");
  String value = getValue (part);
  if (value.equals ("1")){
      doSomethingWithFile1 (request.getPart ("file1"))
  }else if (value.equals (2)){
      doSomethingWithFile2 (request.getPart ("file2"))
  }
  
  private String getValue (Part part) throws IOException{
      if (part == null) return null;
  
      BufferedReader in = null;
      try{
          in = new BufferedReader (new InputStreamReader (part.getInputStream (), request.getCharacterEncoding ()));
      }catch (UnsupportedEncodingException e){}
  
      StringBuilder value = new StringBuilder ();
      char[] buffer = new char[1024];
      for (int bytesRead; (bytesRead = in.read (buffer)) != -1;) {
          value.append (buffer, 0, bytesRead);
      }
  
      return value.toString ();
  }
  </code></pre>
  
  <p>I can't do this because if the client sends a file that exceeds the max size the first call to getPart will throw the exception (see <a href="http://download.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getPart%28java.lang.String%29" rel="nofollow noreferrer">getPart() Javadoc</a>), so I can't know which file I've received.</p>
  
  <p>That's why I want to read the content-disposition headers. I want to read the parameter "param" to know which file has thrown the exception.</p>
  
  <hr>
  
  <p><strong>EDIT2</strong>: Well, with the API that publishes the Servlet 3.0 specification you can't control the previous case because if a file throws an exception you can't read the file field name. This is the negative part of using a wrapper because a lot of functionalities disappear... Also with FileUpload you can dynamically set the MultipartConfig annotation.</p>
  
  <p>If the file exceeds the maximum file size the api throws a <a href="http://commons.apache.org/fileupload/apidocs/org/apache/commons/fileupload/FileUploadBase.FileSizeLimitExceededException.html" rel="nofollow noreferrer">FileSizeLimitExceededException</a> exception. The exception provides 2 methods to get the field name and the file name.</p>
  
  <p>But!! my problem is not still solved because <strong><em>I want to read the value of another parameter sent together with the file in the same form.</em></strong> (the value of "param" in the previous example)</p>
  
  <hr>
  
  <p><strong>EDIT3</strong>: I'm working on this. As soon as I write the code I'll publish it here!</p>