我想使用Spring MVC和Thymeleaf上傳文件,但我收到一個異常說多部分配置沒有提供。Spring MVC文件上傳:無法處理部分,因爲沒有提供多部分配置
這是我Thymeleaf形式:
<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>
這是我的表單綁定類:
: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
}
另外,我還如文檔中我WebMvcConfig.java
這樣說配置的multipart解析器
@Bean
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setDefaultEncoding("utf-8");
resolver.setMaxUploadSize(10000);
return resolver;
}
爲什麼上傳表單不工作,即使我有設置我的MultiPartResolver?我錯過了什麼嗎?
看看https://stackoverflow.com/a/24267170/755401 – arthur