简体   繁体   中英

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.

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.

So, theoretically, It should work without any additional configurations, but it looks like this MultipartConfigElement is not being configured at all.

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:

@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

And create CommonsMultipartResolver bean as (name = "multipartResolver")

Same question,but I got these files by this way.

You can found these files in this github repository:

gs-uploading-files

All you need to do is just download the zip file of this application,and find the files you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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