我正在尝试使用Spring MVC和Thymeleaf上传文件,但我得到一个例外,说没有提供多部分配置。
这是我的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,上传表单也无法正常工作?我错过了什么吗?
答案 0 :(得分:1)
除了没有找到多部件配置之外,它是直接的。虽然您提供了multipartResolver bean。
问题是,在Spring Security过滤器之前指定 MultipartFilter 时,它会尝试获取 multipartResolver 豆但无法找到它。因为它希望bean名称/ id为 filterMultipartResolver 而不是 multipartResolver 。
帮自己一个忙。请更改bean配置,如下所示 -
@Bean
public CommonsMultipartResolver filterMultipartResolver() {
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setDefaultEncoding("utf-8");
resolver.setMaxUploadSize(10000);
return resolver;
}
或
@Bean(name = "filterMultipartResolver")
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setDefaultEncoding("utf-8");
resolver.setMaxUploadSize(10000);
return resolver;
}
答案 1 :(得分:0)
我终于修好了。对于遇到相同问题的任何人,解决方案是在我的WebAbbInitializer.java
中注册多部分配置,添加此自定义方法:
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;
}
最后,将其注册在同一个文件中:
@Override
protected void customizeRegistration(ServletRegistration.Dynamic registration) {
registration.setMultipartConfig(getMultipartConfigElement());
}
您可以阅读有关here.
的更多信息