1

I'm trying to upload a file using Spring MVC and Thymeleaf but I'm getting an exception saying that multi-part configuration has not been provided.

This is my Thymeleaf Form:

<form action="#" th:action="@{/settings/profile}"
    th:object="${profileSettingsForm}" method="POST" enctype="multipart/form-data">
    <div class="form-group">
        <label for="profilePicture">Picture</label> <input type="file"
            th:field="*{profilePicture}" id="profilePicture" name="profilePicture">
    </div>
    <div class="form-group">
        <label for="username">Username</label> <input type="text"
            th:field="*{username}" class="form-control" id="username"
            placeholder="Type your new username">
    </div>
    <div class="form-group">
        <label for="biography">Biography</label>
        <textarea th:field="*{biography}" class="form-control" id="biography"
            rows="3" placeholder="Type your new biography"></textarea>
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
</form>

This is my form binding class:

public class ProfileSettingsForm {

    private static final String NOT_BLANK_MESSAGE = "{notBlank.message}";

    private MultipartFile profilePicture;

    @NotBlank(message = ProfileSettingsForm.NOT_BLANK_MESSAGE)
    private String username;

    @NotBlank(message = ProfileSettingsForm.NOT_BLANK_MESSAGE)
    private String biography;

    public ProfileSettingsForm() {

    }

    public ProfileSettingsForm(String username, String biography) {
        this.username = username;
        this.biography = biography;
    }

    // Getters and setters

}

Also, I have configured the Multipart resolver as the documentation says in my WebMvcConfig.java like this:

@Bean
public CommonsMultipartResolver multipartResolver() {
    CommonsMultipartResolver resolver = new CommonsMultipartResolver();
    resolver.setDefaultEncoding("utf-8");
    resolver.setMaxUploadSize(10000);
    return resolver;
}

Why is the upload form not working even when I have set up my MultiPartResolver? Am I missing something?

Elias Garcia
  • 6,772
  • 11
  • 34
  • 62

2 Answers2

2

It is straight forward from the exception that no multi-part configuration is found. Though you have provided multipartResolver bean.

The problem is that while specifying the MultipartFilter before the Spring Security filter, It tries to get the multipartResolver bean but can't find it. Because it expect the bean name/id as filterMultipartResolver instead of multipartResolver.

Do yourself a favor. Please change the bean configuration like following -

@Bean
public CommonsMultipartResolver filterMultipartResolver() {
    CommonsMultipartResolver resolver = new CommonsMultipartResolver();
    resolver.setDefaultEncoding("utf-8");
    resolver.setMaxUploadSize(10000);
    return resolver;
}

Or

@Bean(name = "filterMultipartResolver")
public CommonsMultipartResolver multipartResolver() {
    CommonsMultipartResolver resolver = new CommonsMultipartResolver();
    resolver.setDefaultEncoding("utf-8");
    resolver.setMaxUploadSize(10000);
    return resolver;
}
Ramjan Ali
  • 510
  • 3
  • 12
1

I finally fixed it. For anyone who is having the same issue, the solution was to register the multipart configuration in my WebAbbInitializer.java, adding this custom method:

private static final String LOCATION = "C:/temp/"; // Temporary location where files will be stored

private static final long MAX_FILE_SIZE = 5242880; // 5MB : Max file size.
                                                   // Beyond that size spring will throw exception.
private static final long MAX_REQUEST_SIZE = 20971520; // 20MB : Total request size containing Multi part.
private static final int FILE_SIZE_THRESHOLD = 0; // Size threshold after which files will be written to disk
private MultipartConfigElement getMultipartConfigElement() {

MultipartConfigElement multipartConfigElement = new MultipartConfigElement(
            LOCATION, MAX_FILE_SIZE, MAX_REQUEST_SIZE, FILE_SIZE_THRESHOLD);
    return multipartConfigElement;
}

Finally, register it in the same file like this:

@Override
protected void customizeRegistration(ServletRegistration.Dynamic registration) {
    registration.setMultipartConfig(getMultipartConfigElement());
}

You can read more about here.

Florian Fankhauser
  • 3,615
  • 2
  • 26
  • 30
Elias Garcia
  • 6,772
  • 11
  • 34
  • 62