Skip to content

Apache Commons ServletFileUpload

Apache Commons ServletFileUpload is a component of the Apache Commons FileUpload library used to handle multipart/form-data requests in Java web applications. It facilitates the parsing of HTTP requests containing file uploads and standard form fields.^[600-developer-frontend-jquery-jquery-validation-file-upload.md]

Configuration and Initialization

To use ServletFileUpload, a DiskFileItemFactory must first be instantiated.^[600-developer-frontend-jquery-jquery-validation-file-upload.md] This factory is responsible for creating new file items and managing the temporary storage repository.^[600-developer-frontend-jquery-jquery-validation-file-upload.md]

Typically, the repository is set to the system's default temporary directory:

DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
^[600-developer-frontend-jquery-jquery-validation-file-upload.md]

A ServletFileUpload object is then created using this factory.^[600-developer-frontend-jquery-jquery-validation-file-upload.md] Additionally, header encoding—such as UTF-8—should be set on the uploader instance to ensure correct handling of file names and form field values.^[600-developer-frontend-jquery-jquery-validation-file-upload.md]

ServletFileUpload sfu = new ServletFileUpload(factory);
sfu.setHeaderEncoding("UTF-8");
^[600-developer-frontend-jquery-jquery-validation-file-upload.md]

Processing the Request

The parseRequest method is used to process the HttpServletRequest, returning a list of FileItem objects.^[600-developer-frontend-jquery-jquery-validation-file-upload.md]

List<FileItem> fis = sfu.parseRequest(request);
^[600-developer-frontend-jquery-jquery-validation-file-upload.md]

These items can be iterated over to access the content of the request.^[600-developer-frontend-jquery-jquery-validation-file-upload.md]

Handling Form Fields

Each FileItem represents either a standard form field or an uploaded file.^[600-developer-frontend-jquery-jquery-validation-file-upload.md] The isFormField() method distinguishes between the two types.^[600-developer-frontend-jquery-jquery-validation-file-upload.md]

For standard form fields (where isFormField() returns true), the field name and value can be retrieved using the field name and specifying the character set.^[600-developer-frontend-jquery-jquery-validation-file-upload.md]

String fieldName = fi.getFieldName();
String fieldValue = fi.getString("UTF-8");
^[600-developer-frontend-jquery-jquery-validation-file-upload.md]

Handling Uploaded Files

For uploaded files (where isFormField() returns false), various metadata and content can be accessed, including the original file name, the field name, and the file extension (often retrieved using utilities like FilenameUtils).^[600-developer-frontend-jquery-jquery-validation-file-upload.md]

The binary content of the file can be read from the item's input stream.^[600-developer-frontend-jquery-jquery-validation-file-upload.md]

String uploadFileName = fi.getName();
String fieldName = fi.getFieldName();
byte[] bytes = IOUtils.toByteArray(fi.getInputStream());
^[600-developer-frontend-jquery-jquery-validation-file-upload.md]

Sources

  • 600-developer-frontend-jquery-jquery-validation-file-upload.md