Spring’s multipart support [FileUpload]
Posted: December 9, 2014 Filed under: Java, Spring Framework | Tags: Commons, FileUpload, java, Multipart, Spring Framework Leave a commentBy default Spring does no multipart handling. You enable multipart support by adding MultipartResolver interface, defined in the org.springframework.web.multipart package, in the web application’s context. Out of the box, Spring provides a MultipartResolver object for use with Commons FileUpload. But make sure you have appropraite jars in your class path for the multipart resolver to work. In the case of the CommonsMultiparResolver, you need to use commons-fileupload.jar.
Each request is inspected to see if it contains a multipart . If no multipart is found, the request continues as normal. When the Spring’s DispatcherServlet detects a multipart request, it activates the resolver that has been declared in your context and hands over the request. The resolver then wraps the current HttpServletRequest into a MultipartHttpServletRequest that supports multipart file uploads. Using the MultipartHttpServletRequest you can get information about the multiparts contained by this request and actually get access to the multipart files themselves in your controllers.
My Controller class
package com.vaisakh.fileupload; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; /** * Handles requests for the application file upload requests */ @Controller public class UploadController { /** * Upload single file using Spring Controller */ @RequestMapping(value = "/uploadFile", method = RequestMethod.POST) public @ResponseBody String uploadFileHandler(@RequestParam("name") String name, @RequestParam("file") MultipartFile file) { if (!file.isEmpty()) { try { byte[] bytes = file.getBytes(); // Creating the directory to store file String rootPath = System.getProperty("catalina.home"); File dir = new File(rootPath + File.separator + "tmpFiles"); if (!dir.exists()) dir.mkdirs(); // Create the file on server File serverFile = new File(dir.getAbsolutePath() + File.separator + name); BufferedOutputStream stream = new BufferedOutputStream( new FileOutputStream(serverFile)); stream.write(bytes); stream.close(); logger.info("Server File Location=" + serverFile.getAbsolutePath()); return "You successfully uploaded file=" + name; } catch (Exception e) { return "You failed to upload " + name + " => " + e.getMessage(); } } else { return "You failed to upload " + name + " because the file was empty."; } } }
Below is my configuration file for DispatcherServlet
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- DispatcherServlet context: defines this servlet's request-processing infrastructure --> <!-- Enables the spring MVC @Controller programming model --> <mvc:annotation-driven></mvc:annotation-driven> <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> <mvc:resources location="/resources/" mapping="/resources/**" /> <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- Lets use the Commons-based implementation of the MultipartResolver Interface --> <bean id="MultipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean> <context:component-scan base-package="com.vaisakh.fileupload"></context:component-scan> </beans>