title: FileUpload ERROR
body: <p>i have the following code to upload files:</p>
  
  <pre><code>package upload;
  
  import java.io.File;
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.util.Iterator;
  import java.util.List;
  
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import org.apache.tomcat.util.http.fileupload.FileItem;
  import org.apache.tomcat.util.http.fileupload.RequestContext;
  import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
  import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
  
  @WebServlet("/uploadServer")
  public class uploadServer extends HttpServlet {
  private static final long serialVersionUID = 1L;
  
  private static final String UPLOAD_DIRECTORY = "uploads";
  private static final int THRESHOLD_SIZE     = 1024 * 1024 * 3;  // 3MB
  private static final int MAX_FILE_SIZE      = 1024 * 1024 * 40; // 40MB
  private static final int MAX_REQUEST_SIZE   = 1024 * 1024 * 50; // 50MB
  
  public uploadServer() {
      super();
  
  }
  
  
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      doPost(request, response);
  }
  
  
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
        // checks if the request actually contains upload file
      if (!ServletFileUpload.isMultipartContent(request)) {
          PrintWriter writer = response.getWriter();
          writer.println("Request does not contain upload data");
          writer.flush();
          return;
      }
  
      // configures upload settings
      DiskFileItemFactory factory = new DiskFileItemFactory();
      factory.setSizeThreshold(THRESHOLD_SIZE);
      factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
  
      ServletFileUpload upload = new ServletFileUpload(factory);
      upload.setFileSizeMax(MAX_FILE_SIZE);
      upload.setSizeMax(MAX_REQUEST_SIZE);
  
      // constructs the directory path to store upload file
      String uploadPath = getServletContext().getRealPath("")
          + File.separator + UPLOAD_DIRECTORY;
      // creates the directory if it does not exist
      File uploadDir = new File(uploadPath);
      if (!uploadDir.exists()) {
          uploadDir.mkdir();
      }
  
      try {
          // parses the request's content to extract file data
          List formItems = upload.parseRequest((RequestContext) request);
          Iterator iter = formItems.iterator();
  
          // iterates over form's fields
          while (iter.hasNext()) {
              FileItem item = (FileItem) iter.next();
              // processes only fields that are not form fields
              if (!item.isFormField()) {
                  String fileName = new File(item.getName()).getName();
                  String filePath = uploadPath + File.separator + fileName;
                  File storeFile = new File(filePath);
  
                  // saves the file on disk
                  item.write(storeFile);
              }
          }
          request.setAttribute("message", "Upload has been done successfully!");
      } catch (Exception ex) {
          request.setAttribute("message", "There was an error: " + ex.getMessage());
      }
      getServletContext().getRequestDispatcher("/message.jsp").forward(request, response);
  }
  }
  </code></pre>
  
  <p>And the HTML file looks like this:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Upload Files using XMLHttpRequest - Minimal&lt;/title&gt;
  
  &lt;script type="text/javascript"&gt;
  
    function fileSelected() {
      var file = document.getElementById('fileToUpload').files[0];
      if (file) {
        var fileSize = 0;
        if (file.size &gt; 1024 * 1024)
          fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB';
        else
          fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';
  
        document.getElementById('fileName').innerHTML = 'Name: ' + file.name;
        document.getElementById('fileSize').innerHTML = 'Size: ' + fileSize;
        document.getElementById('fileType').innerHTML = 'Type: ' + file.type;
      }
    }
  
    function uploadFile() {
      var fd = new FormData();
      fd.append("fileToUpload", document.getElementById('fileToUpload').files[0]);
      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", "uploadServer", true);
      xhr.send(fd);
    }
  
    function uploadProgress(evt) {
      if (evt.lengthComputable) {
        var percentComplete = Math.round(evt.loaded * 100 / evt.total);
        document.getElementById('progressNumber').innerHTML = percentComplete.toString() + '%';
      }
      else {
        document.getElementById('progressNumber').innerHTML = 'unable to compute';
      }
    }
  
    function uploadComplete(evt) {
      /* This event is raised when the server send back a response */
      alert(evt.target.responseText);
    }
  
    function uploadFailed(evt) {
      alert("There was an error attempting to upload the file.");
    }
  
    function uploadCanceled(evt) {
      alert("The upload has been canceled by the user or the browser dropped the connection.");
    }
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form id="form1" enctype="multipart/form-data" method="post" action="Upload.aspx"&gt;
  &lt;div class="row"&gt;
    &lt;label for="fileToUpload"&gt;Select a File to Upload&lt;/label&gt;&lt;br /&gt;
    &lt;input type="file" name="fileToUpload" id="fileToUpload" onchange="fileSelected();"/&gt;
  &lt;/div&gt;
  &lt;div id="fileName"&gt;&lt;/div&gt;
  &lt;div id="fileSize"&gt;&lt;/div&gt;
  &lt;div id="fileType"&gt;&lt;/div&gt;
  &lt;div class="row"&gt;
    &lt;input type="button" onclick="uploadFile()" value="Upload" /&gt;
  &lt;/div&gt;
  &lt;div id="progressNumber"&gt;&lt;/div&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>But when i run the programm and try to upload some file i get the following error message:</p>
  
  <p><strong>There was an error: org.apache.catalina.connector.RequestFacade cannot be cast to org.apache.tomcat.util.http.fileupload.RequestContext</strong></p>
  
  <p>Can you help me, why i get this message and what i should change to run the programm </p>
  
  <p>Thanks</p>