2016-09-29 60 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に言うように、私はマルチパートリゾルバを設定しています

@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

私は最終的にそれを修正しました。

@Override 
protected void customizeRegistration(ServletRegistration.Dynamic registration) { 
    registration.setMultipartConfig(getMultipartConfigElement()); 
} 
:このように同じファイルに登録し、最後に

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; 
} 

:同じ問題を抱えている人のために、解決策は、このカスタムメソッドを追加して、私のWebAbbInitializer.javaでマルチパートの設定を登録することでした

詳細を読むことができますhere.

1

これは、マルチパート構成が見つからないという点を除いて簡単です。あなたはmultipartResolver beanを提供しましたが。

問題は、春のセキュリティフィルタの前MultipartFilterを指定して、それは、multipartResolver Beanを取得しようとしますが、それを見つけることができないということです。それは、filterMultipartResolver代わりのmultipartResolverとしてBean名/ IDを期待していますので。

自分で好きですか?次のような豆の設定を変更してください -

@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; 
} 
関連する問題