简体   繁体   English

未为 MultipartFile 配置 Spring Boot 2.0.2 MultipartConfigElement

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

I'm using Spring Boot 2.0.2.RELEASE , and not being able to upload files for a REST controller endpoint.我正在使用 Spring Boot 2.0.2.RELEASE ,并且无法为 REST 控制器端点上传文件。

Following this getting starter, it says:这个入门之后,它说:

As part of auto-configuring Spring MVC, Spring Boot will create a MultipartConfigElement bean and make itself ready for file uploads.作为自动配置 Spring MVC 的一部分,Spring Boot 将创建一个 MultipartConfigElement bean 并为文件上传做好准备。

So, theoretically, It should work without any additional configurations, but it looks like this MultipartConfigElement is not being configured at all.因此,理论上,它应该在没有任何额外配置的情况下工作,但看起来这个MultipartConfigElement根本没有被配置。

I'm getting this warn:我收到这个警告:

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"

My Spring application starter is as simple as:我的 Spring 应用程序启动器非常简单:

@SpringBootApplication
public class MyApplication {

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

}

And my endpoint is:我的终点是:

@RestController
public class MyController {

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

This is the way I'm sending with postman:这是我与邮递员一起发送的方式: 在此处输入图片说明

I also made sure to unmark any default content type set by postman, with no success.我还确保取消标记邮递员设置的任何默认内容类型,但没有成功。 What possibly am I doing wrong?我可能做错了什么?

First, add this to your properties file首先,将其添加到您的属性文件中

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

And create CommonsMultipartResolver bean as (name = "multipartResolver")并将 CommonsMultipartResolver bean 创建为 (name = "multipartResolver")

Same question,but I got these files by this way.同样的问题,但我通过这种方式获得了这些文件。

You can found these files in this github repository:您可以在此 github 存储库中找到这些文件:

gs-uploading-files gs-上传文件

All you need to do is just download the zip file of this application,and find the files you need.您所需要做的就是下载此应用程序的 zip 文件,然后找到您需要的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM