I am using Spring Boot version = \'1.4.0.RC1\' with Spring Boot Stormpath 1.0.2.
I am trying to use multipart file upload but the MultipartFile is always null in the con
You have to enable the Spring Multipart Resolver as by default Spring doesn't enable the multipart capability.
By default, Spring does no multipart handling, because some developers want to handle multiparts themselves. You enable Spring multipart handling by adding a multipart resolver to the web application’s context.
To your configuration class you would want to add the following bean:
@Bean
public MultipartResolver multipartResolver() {
return new CommonsMultipartResolver();
}
* Update * As my previous answer was not correct based on the comments. Here is an updated example that I was able to run successfully.
@SpringBootApplication
public class StackoverflowWebmvcSandboxApplication {
public static void main(String[] args) {
SpringApplication.run(StackoverflowWebmvcSandboxApplication.class, args);
}
@Controller
public class UploadPhoto {
@PostMapping("{username}/profilePhoto")
public ResponseEntity saveProfilePhoto(@PathVariable("username") String username,
@RequestPart(name = "file", required = false) MultipartFile imageFile, HttpServletRequest request) {
String body = "MultipartFile";
if (imageFile == null) {
body = "Null MultipartFile";
}
return ResponseEntity.status(HttpStatus.CREATED).body(body);
}
}
}
It is a very basic test with no special stuff. I then created a postman request and here is the sample curl call:
curl -X POST -H "Cache-Control: no-cache" -H "Postman-Token: 17e5e6ac-3762-7d45-bc99-8cfcb6dc8cb5" -H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" -F "file=@" "http://localhost:8080/test/profilePhoto"
The response was MultipartFile
meaning that it wasn't null and doing a debug on that line showed that the variable was populated with the image that I was uploading.