Class JakartaMultiPartRequest
- All Implemented Interfaces:
MultiPartRequest
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:
-
AbstractMultiPartRequestJakartaServletDiskFileUpload
-
Field Summary
Fields inherited from class org.apache.struts2.dispatcher.multipart.AbstractMultiPartRequest
BUFFER_SIZE, bufferSize, defaultEncoding, errors, maxFiles, maxFileSize, maxSize, maxSizeOfFiles, maxStringLength, parameters, STRUTS_MESSAGES_UPLOAD_ERROR_PARAMETER_TOO_LONG_KEY, uploadedFiles -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidcleanUp()Performs complete cleanup of all resources associated with this request.protected voidCleans up disk file items by deleting associated temporary files.protected voidCleans up temporary files created for in-memory uploads.protected voidprocessFileField(org.apache.commons.fileupload2.core.DiskFileItem item, String saveDir) Processes a file field from the multipart request.protected voidprocessNormalFormField(org.apache.commons.fileupload2.core.DiskFileItem item, Charset charset) Processes a normal form field (non-file) from the multipart request.protected voidprocessUpload(jakarta.servlet.http.HttpServletRequest request, String saveDir) Processes the multipart upload request using Jakarta Commons FileUpload.Methods inherited from class org.apache.struts2.dispatcher.multipart.AbstractMultiPartRequest
buildErrorMessage, createJakartaFileUpload, createRequestContext, createTemporaryFile, deleteFile, exceedsMaxStringLength, getCanonicalName, getContentType, getErrors, getFile, getFileNames, getFileParameterNames, getFilesystemName, getParameter, getParameterNames, getParameterValues, parse, prepareServletFileUpload, readCharsetEncoding, rejectEmptyFile, setBufferSize, setDefaultEncoding, setMaxFiles, setMaxFileSize, setMaxSize, setMaxSizeOfFiles, setMaxStringLength
-
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:
- Reading the character encoding from the request
- Preparing the Jakarta servlet file upload handler
- Creating a request context for processing
- Iterating through all form items (fields and files)
- Processing each item appropriately based on its type
All
DiskFileIteminstances are automatically tracked for proper cleanup.- Specified by:
processUploadin classAbstractMultiPartRequest- Parameters:
request- the HTTP servlet request containing the multipart datasaveDir- 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:
- Validating the field name is not null
- Extracting the field value using the specified charset
- Checking if the field value exceeds maximum string length
- 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 fieldcharset- 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:
- Validating the file name and field name are not null/empty
- Determining if the file is stored in memory or on disk
- For in-memory files: creating a temporary file and copying content
- For disk files: using the existing file directly
- Creating an
UploadedFileabstraction - 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
DiskFileIteminstances 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:
- Iterates through all tracked temporary files
- Checks if each file still exists
- Attempts to delete existing files
- 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:
- Calls parent cleanup to handle base class resources
- Cleans up all tracked
DiskFileIteminstances - Cleans up all temporary files created for in-memory uploads
- 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:
cleanUpin interfaceMultiPartRequest- Overrides:
cleanUpin classAbstractMultiPartRequest- See Also:
-