Search code examples
javaspringspring-bootspring-mvcthymeleaf

Uploading Multiple Files with Spring Boot and Thymeleaf


I have a SpringBoot + Thymeleaf application:

This is my bean:

public class UserPayload implements Serializable {

    // pics

    private MultipartFile file1;
    private MultipartFile file2;
    private MultipartFile file3;
    private MultipartFile file4;
    private MultipartFile file5;
    private MultipartFile file6;
    private MultipartFile file7;
    private MultipartFile file8;
    private MultipartFile file9;
    private MultipartFile file10;
}

the template:

 <!-- File -->
            <div class="form-group textogbcn">
             
                <input type="file" id="file1" class="form-control" th:field="*{file1}" accept=".gif, .jpg, .png, .jpeg" />
            </div>

            <!-- File -->
            <div class="form-group textogbcn">
   
                <input type="file" id="file2" class="form-control" th:field="*{file2}" accept=".gif, .jpg, .png, .jpeg" />
            </div>

            <!-- File -->
            <div class="form-group textogbcn">
  
                <input type="file" id="file3" class="form-control" th:field="*{file3}" accept=".gif, .jpg, .png, .jpeg" />
            </div>

..

</form>

but when I upload severeal files, only the first is not null, the others are null in the controller


Solution

  • This way. Try to fix controller. Get a request for a list.

    public List upload(@RequestPart List files)