Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Spring Spring3.2 Multipart support for file upload in Spring MVC

Related Tutorials


 
 

Share on Google+Share on Google+

Multipart support for file upload in Spring MVC

Advertisement
In this section, you will learn about 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";
	}

}

 

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 12, 2013

Related Tutorials

Discuss: Multipart support for file upload in Spring MVC  

Post your Comment


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