簡體   English   中英

未為 MultipartFile 配置 Spring Boot 2.0.2 MultipartConfigElement

[英]Spring Boot 2.0.2 MultipartConfigElement not being configured for MultipartFile

我正在使用 Spring Boot 2.0.2.RELEASE ,並且無法為 REST 控制器端點上傳文件。

這個入門之后,它說:

作為自動配置 Spring MVC 的一部分,Spring Boot 將創建一個 MultipartConfigElement bean 並為文件上傳做好准備。

因此,理論上,它應該在沒有任何額外配置的情況下工作,但看起來這個MultipartConfigElement根本沒有被配置。

我收到這個警告:

WARN .a.w.r.e.DefaultErrorWebExceptionHandler: Failed to handle request [POST http://localhost:8080/upload]: Response status 400 with reason "Required MultipartFile parameter 'file' is not present"

我的 Spring 應用程序啟動器非常簡單:

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}

我的終點是:

@RestController
public class MyController {

@PostMapping("/upload")
public String hash(@RequestParam("file") MultipartFile file) {
    final String test = file.getContentType();
}

這是我與郵遞員一起發送的方式: 在此處輸入圖片說明

我還確保取消標記郵遞員設置的任何默認內容類型,但沒有成功。 我可能做錯了什么?

首先,將其添加到您的屬性文件中

servlet.multipart.enabled=true servlet.multipart.max-file-size=20M

並將 CommonsMultipartResolver bean 創建為 (name = "multipartResolver")

同樣的問題,但我通過這種方式獲得了這些文件。

您可以在此 github 存儲庫中找到這些文件:

gs-上傳文件

您所需要做的就是下載此應用程序的 zip 文件,然后找到您需要的文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM