I wrote a little sequel to Java Config with Spring MVC. In addition to the contents set in Java Config last time, this time we will set the file upload setting of Spring so that it can be uploaded.
We will register MultipartResolver as a bean in the setting class. You can set the upper limit of memory size and upload size at the same time when registering. When making this setting as a FW migration, the same behavior can be reproduced by checking and setting the original setting value.
private static final int MAX_UPLOAD_SIZE = 1024 * 1024; //1MB
private static final int MAX_IN_MEMORY_SIZE = 1024 * 256; //256KB
@Bean
public MultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(MAX_UPLOAD_SIZE);
multipartResolver.setMaxInMemorySize(MAX_IN_MEMORY_SIZE);
return multipartResolver;
}
The property to be uploaded to the file defines the type as MultipartFile.
/**
*File to upload
*/
private MultipartFile file = null;
After that, you can actually operate the file by adding getters and setters to the above properties and uploading the file in cooperation with View.
As a procedure, you can easily set it as described above. I remember it took a long time to set it up for the first time, so I hope it helps someone!
Recommended Posts