Multipart support for file upload in Spring MVC

In this section, you will learn about multipart support for file upload in Spring MVC.

Multipart support for file upload in Spring MVC

Multipart support for file upload in Spring MVC

In this section, you will learn about multipart support for file upload in Spring MVC.

Spring have built in support for uploading file in a web application. Using MultipartResolver object of org.springframework.web.multipart package, you can enable multipart support. There are two implementation of MultipartResolver - first for Commons FileUpload and other for Servlet 3.0 multipart request parsing.

There is no default implementation of the MultipartResolver because some people want to handle multiparts at their own. If you want to use it, you need to add MultipartResolver to the web application's context.

MultipartResolver with Commons FileUpload

You can add CommonsMultipartResolver to the web application's context as follows :

<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
	<property name="maxUploadSize" value="100000"/>
</bean>

The value of the maximum upload size(maxUploadSize) is given in bytes. You need to add commons-fileupload.jar, if you are using CommonsMultipartResolver.

You must add enctype="multipart/form-data" in <form> tag to a support encoding of the form as multipart request. After configuring MultipartResolver, you need to create a form having file input for uploading :

<form method="post" action="/form" enctype="multipart/form-data">
	<input type="file" name="file"/>
	<input type="submit"/>
</form>

After creating form, you need to create controller which handles file uploading as follows :

@Controller
public class ControllerFileHandling {

	@RequestMapping(value = "/form", method = RequestMethod.POST)
	public String FormHandling(@RequestParam("name") String name,
	@RequestParam("file") MultipartFile file) {
	
		if (!file.isEmpty()) {
			byte[] bytes = file.getBytes();
			// store the bytes in a database or file system and so on
			return "redirect:fileuploadSuccess";
		} else {
			return "redirect:fileuploadFailure";
		}
	}

}

You can see in above example, request parameter 'file' is saved in 'file' parameter of type MultipartFile.

MultipartResolver with Servlet 3.0

For using Servlet 3.0 based multipart parsing, you need to do one of the below things :

  • Define DispatcherServlet with a "multipart-config" section in web.xml.
  • Define DispatcherServlet with a javax.servlet.MultipartConfigElement in programmatic Servlet registration.
  • In case of a custom Servlet class possibly with a javax.servlet.annotation.MultipartConfig annotation on your Servlet class.

You need to add StandardServletMultipartResolver to Spring configuration as follows :

 <bean id="multipartResolver"
class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
</bean>

Setting for maximum sizes or storage locations does not allow to be done from the MultipartResolver, as shown above. These setting need to be done at Servlet registration level as Servlet 3.0.

In case of Servlet 3.0 multipart parsing, the Controller should be like this :

@Controller
public class ControllerFileHandling {

	@RequestMapping(value = "/form", method = RequestMethod.POST)
	public String FormHandling(@RequestParam("name") String name,
	@RequestParam("file") Part file) {
	
		InputStream inputStream = file.getInputStream();
		// store bytes from uploaded file in a database or file system and so on
		
		
		return "redirect:fileuploadSuccess";
	}

}