Multipart support for file upload in Spring MVC

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

Ads

Tutorials   
Spring 3.2 MVC insert and retrieve blob from the database The hidden tag The errors tag net.roseindia.dao net.roseindia.service net.roseindia.model net.roseindia.controller Spring 3.2 MVC Hibernate Example Spring 3.2 MVC, Upload File in a specific folder Spring 3.2 MVC Form Handling The textarea tag The options tag The option tag The select tag The radiobuttons tag The radiobutton tag The checkboxes tag mvc:default-servlet-handler Static Resources Configuration View Controllers Configuration View resolvers Spring 3.2 MVC Hello World Example Content Negotiation Configuration Interceptors Configuration using Java or XML Customizing the MVC Java config or XML Namespace MVC Java Config or the MVC XML Namespace Servlet container initialization through code Support for ETag The checkbox tag Autogenerated logical view name through RequestToViewNameTranslator Modification in conventional ModelAndView ControllerClassNameHandlerMapping class for handling convention mapping The password tag The input tag The form tag Form Tag library configuration Customizing the Default Error Page @ExceptionHandler & @ResponseStatus annotation Multipart support for file upload in Spring MVC Themes and Theme resolvers in Spring MVC Locales in Spring MVC ContentNegotiatingViewResolver Redirecting and forwarding to views Chaining of Multiple view resolvers View Resolving through ViewResolver interface Requests Intercepting through a HandlerInterceptor Defining handler methods using @RequestMapping annotation Spring 3.2 Asynchronous Request Processing @RequestMapping annotation for mapping requests @Controller annotation for defining new controller
Ads

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

}

 

Advertisements

Share on Google+Share on Google+

Multipart support for file upload in Spring MVC

Posted on: January 12, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Multipart support for file upload in Spring MVC  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads