Last Update: 23.05.2017. By Jens in Spring Boot | Spring MVC
Uploading a file with Spring MVC is pretty easy, and it gets even easier when running in Spring Boot. However, it’s one of the tasks a developer does once in a while and therefore is pretty easy to forget. The page is a reminder for all of us.
For uploading a file, you must use multipart/form-data as the form encoding and usually files are uploaded with a POST request.
On the Spring MVC @Controller we only need to add a @RequestParam with the name of your form field and map it to a MultipartFile class.
The MultipartFile class provides access to the file, file name, file type, etc.
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
public void upload(@RequestParam("file") MultipartFile file, @RequestParam String additionalField) {
System.out.println(String.format("File name %s", file.getName()));
System.out.println(String.format("File original name %s", file.getOriginalFilename()));
System.out.println(String.format("File size %s", file.getSize()));
//do whatever you want with the MultipartFile
file.getInputStream();
}
For uploading multiple files together, we only need to add all the files to the form.
In your form will have multiple instances of file inputs using the same name attribute. The browser will include them all in the request.
<input type="file" name="files" />
<input type="file" name="files" />
<input type="file" name="files" />
In the @Controller, we can simply switch from a single MultipartFile parameter to using a MultipartFile array.
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
public void upload(@RequestParam("file") MultipartFile[] file, @RequestParam String additionalField) {
//do whatever you want with the files
}
In Spring Boot the Multipart file upload is configured automatically and set up with defaults. It will also use the Servlet 3 API for Multiparts.
If you need to adjust the max file size or similar, you can do so by setting the following properties in your application.properties.
The most used properties are:
Uploading files with Spring MVC is straight forward, and with Spring Boot you get zero configuration too. And, in case you must change the file size or other, you can do so easily with the properties.