Using FileUpload

Your application should detect whether or not FileUpload should be invoked, based on the HTTP method and the content type of the request.

Assuming that you have decided that FileUpload should be invoked, you might write the following code to handle a file upload request:

    // Create a new file upload handler
    DiskFileUpload upload = new DiskFileUpload();

    // Set upload parameters
    upload.setSizeMax(MAX_UPLOAD_SIZE);
    upload.setSizeThreshold(MAX_MEMORY_SIZE);
    upload.setRepositoryPath(TEMP_DIR);

    // Parse the request
    List items = upload.parseRequest(request);

    // Process the uploaded fields
    Iterator iter = items.iterator();
    while (iter.hasNext()) {
        FileItem item = (FileItem) iter.next();

        if (item.isFormField()) {
            processTextParameter(request, item);
        } else {
            processFileParameter(request, item);
        }
    }