Uploading multiple files from page controllers

I’m trying to upload multiple files from my module. Initially my controller signature looked like below

public String post(HttpServletRequest request, PageModel model, UiUtils uiUtils) {

}

Retrieving the files from the HttpServletRequest using request.getParts was not working. The methods HttpServletRequest.getPart and HttpServletRequest.getParts are not been resolved and the interface javax.servlet.http.Part is not in my class path. So I checked the api docs for servlets and it says the HttpServletRequest.getParts method was introduced since Servlet 3.0. My module uses openmrs 2.0.6 so i checked the 2.0.x branch on GitHub to see which version of the API is been used. As you can see below, it shows version 3.0.1 is been used

But I’m not sure why those methods and the javax.servlet.Part interface are not been resolved.

So in search of a possible solution on openmrs talk and wiki, I came across Flexible Method Signatures for UI Framework Controller and Action Methods

And under this section @RequestParam annotation, it says

You may put the @org.springframework.web.bind.annotation.RequestParam annotation (from Spring MVC) on any parameter and the UI Framework will take the specified parameter from the HTTP request, and use Spring’s ConversionService to convert it to the specified argument type.

And gives this example

public void controller(@RequestParam("patientId") Patient patient) { ... }

So I modified my controller signature to

public String post(HttpServletRequest request, PageModel model, UiUtils uiUtils, @RequestParam("files") MultipartFile[] files) {

}

For some reason, the files array is always containing a single element even when I upload multiple files. I tried changing the array to a List but I still get the same result.

I’ve run my code through a debugger and it shows that just one file is contained in the array. I’m not sure what is happening

My view looks something like this

<form class="simple-form-ui" id="patientRegistrationForm" method="post" enctype="multipart/form-data">
    // form elements
       
    <input type="file" name="files" multiple="true"/>

    <input type="submit" value="Enter form"/>
</form>