Class JakartaMultiPartRequest

java.lang.Object
org.apache.struts2.dispatcher.multipart.AbstractMultiPartRequest
org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest
All Implemented Interfaces:
MultiPartRequest

public class JakartaMultiPartRequest extends AbstractMultiPartRequest
Multipart form data request adapter for Jakarta Commons FileUpload package.

This implementation provides secure handling of multipart requests with proper resource management and cleanup. It tracks all temporary files created during the upload process and ensures they are properly cleaned up to prevent resource leaks and security vulnerabilities.

Key features:

  • Automatic tracking and cleanup of temporary files
  • Proper error handling with user-friendly error messages
  • Support for both in-memory and disk-based file uploads
  • Extensible cleanup mechanisms for customization

Usage example:

 JakartaMultiPartRequest multipartRequest = new JakartaMultiPartRequest();
 try {
     multipartRequest.parse(request, "/tmp/uploads");
     // Process uploaded files
     for (String fieldName : multipartRequest.getFileParameterNames()) {
         List<UploadedFile> files = multipartRequest.getFile(fieldName);
         // Handle files
     }
 } finally {
     multipartRequest.cleanUp(); // Always clean up resources
 }
 
See Also:
  • Constructor Details

    • JakartaMultiPartRequest

      public JakartaMultiPartRequest()
  • Method Details

    • processUpload

      protected void processUpload(jakarta.servlet.http.HttpServletRequest request, String saveDir) throws IOException
      Processes the multipart upload request using Jakarta Commons FileUpload.

      This method handles the core upload processing by:

      1. Reading the character encoding from the request
      2. Preparing the Jakarta servlet file upload handler
      3. Creating a request context for processing
      4. Iterating through all form items (fields and files)
      5. Processing each item appropriately based on its type

      All DiskFileItem instances are automatically tracked for proper cleanup.

      Specified by:
      processUpload in class AbstractMultiPartRequest
      Parameters:
      request - the HTTP servlet request containing the multipart data
      saveDir - the directory where uploaded files will be stored
      Throws:
      IOException - if an error occurs during upload processing
      See Also:
    • processNormalFormField

      protected void processNormalFormField(org.apache.commons.fileupload2.core.DiskFileItem item, Charset charset) throws IOException
      Processes a normal form field (non-file) from the multipart request.

      This method handles text form fields by:

      1. Validating the field name is not null
      2. Extracting the field value using the specified charset
      3. Checking if the field value exceeds maximum string length
      4. Adding the value to the parameters map

      Fields with null names are skipped with a warning log message.

      Empty form fields are stored as empty strings.

      Parameters:
      item - the disk file item representing the form field
      charset - the character set to use for decoding the field value
      Throws:
      IOException - if an error occurs reading the field value
      See Also:
    • processFileField

      protected void processFileField(org.apache.commons.fileupload2.core.DiskFileItem item, String saveDir)
      Processes a file field from the multipart request.

      This method handles file uploads by:

      1. Validating the file name and field name are not null/empty
      2. Determining if the file is stored in memory or on disk
      3. For in-memory files: creating a temporary file and copying content
      4. For disk files: using the existing file directly
      5. Creating an UploadedFile abstraction
      6. Adding the file to the uploaded files collection

      Temporary files created for in-memory uploads are automatically tracked for cleanup. Any errors during temporary file creation are logged and added to the error list for user feedback.

      Parameters:
      item - the disk file item representing the uploaded file
      See Also:
    • cleanUpDiskFileItems

      protected void cleanUpDiskFileItems()
      Cleans up disk file items by deleting associated temporary files.

      This method iterates through all tracked DiskFileItem instances and performs cleanup operations:

      • For in-memory items: logs cleanup (no files to delete)
      • For disk items: deletes the associated temporary file

      This method is called automatically during cleanUp() but can be overridden by subclasses to customize cleanup behavior. All exceptions are caught and logged to prevent cleanup failures from affecting the overall cleanup process.

      See Also:
    • cleanUpTemporaryFiles

      protected void cleanUpTemporaryFiles()
      Cleans up temporary files created for in-memory uploads.

      This method deletes all temporary files that were created when processing in-memory uploads. These files are created in processFileField(DiskFileItem, String) when an uploaded file is stored in memory and needs to be written to disk.

      The cleanup process:

      1. Iterates through all tracked temporary files
      2. Checks if each file still exists
      3. Attempts to delete existing files
      4. Logs warnings for files that cannot be deleted

      This method can be overridden by subclasses to customize cleanup behavior. All exceptions are caught and logged to ensure cleanup continues even if individual file deletions fail.

      See Also:
    • cleanUp

      public void cleanUp()
      Performs complete cleanup of all resources associated with this request.

      This method extends the parent cleanup functionality to ensure proper cleanup of Jakarta-specific resources:

      1. Calls parent cleanup to handle base class resources
      2. Cleans up all tracked DiskFileItem instances
      3. Cleans up all temporary files created for in-memory uploads
      4. Clears internal tracking collections

      This method is designed to be safe to call multiple times and will not throw exceptions even if cleanup operations fail. All errors are logged for debugging purposes.

      Important: This method should always be called in a finally block to ensure resources are properly released, even if exceptions occur during request processing.

      Specified by:
      cleanUp in interface MultiPartRequest
      Overrides:
      cleanUp in class AbstractMultiPartRequest
      See Also: