[JAVA] Set up Multipart Resolver to allow file uploads in Spring

Until you can upload files in Spring

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.

Setting method

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;
	}

how to use

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

Set up Multipart Resolver to allow file uploads in Spring
Set up Gradle multi-project in IntelliJ to build JAR file
How to bind to property file in Spring Boot
How to set environment variables in the properties file of Spring boot application
Set context-param in Spring Boot
How to set up a proxy with authentication in Feign
How to set and use profile in annotation-based Configuration in Spring framework
Fix the file name of war to the one set in Maven
How to use Lombok in Spring
How to set Spring Boot + PostgreSQL
Set up pre-login routing in Devise
Log output to file in Java
How to set Lombok in Eclipse
Procedure to make the value of the property file visible in Spring Boot
Set up a Java GUI in a separate thread to keep the main
Set Spring Security authentication result to JSON
Convert request parameter to Enum in Spring
Static file access priority in Spring boot
How to include Spring Tool in Eclipse 4.6.3?
Local file download memorandum in Spring Boot
How to add jar file in ScalaIDE
How to set up and use kapt
To write Response data directly in Spring
Sample to unzip gz file in Java
How to split Spring Boot message file
File output bean as JSON in spring