2016-09-29 61 views
0

我想使用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?我錯過了什麼嗎?

+0

看看https://stackoverflow.com/a/24267170/755401 – arthur

回答

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.

1

從沒有找到多部分配置的例外情況來看,它是直截了當的。儘管你已經提供了multipartResolver bean。

的問題是,雖然Spring Security的過濾器之前指定MultipartFilter,它試圖得到multipartResolver豆,但無法找到它。因爲它期望bean名稱/編號爲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; 
} 
相關問題