Spring framework based on java technology and it's comes with MultipartResolver to handle upload files in web based application. The MultipartResolver has CommonsMultipartResolver which use the Apache commons upload library to handle the upload files in a form.
Follow the below steps and create your own web appliction :
Step 1 : Dependency
We are uploding file, so we need to get the commons-fileupload.jar and commons-io.jar libraries.
<!-- Spring framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>
<!-- Spring MVC framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>2.5.6</version>
</dependency>
<!-- Apache Commons Upload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<!-- Apache Commons Upload -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
Step 2 : Model
Create FileUpload.java file and put below code.
package com.bhagwan.common.model;
import org.springframework.web.multipart.MultipartFile;
public class FileUpload{
MultipartFile file;
//getter and setter methods
}
Step 3 : Controller
Create FileUploadController.java and put below code.
package com.bhagwan.common.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import com.bhagwan.common.model.FileUpload;
public class FileUploadController extends SimpleFormController{
public FileUploadController(){
setCommandClass(FileUpload.class);
setCommandName("fileUploadForm");
}
@Override
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
FileUpload file = (FileUpload)command;
MultipartFile multipartFile = file.getFile();
String fileName="";
if(multipartFile!=null){
fileName = multipartFile.getOriginalFilename();
//do whatever you want
}
return new ModelAndView("FileUploadSuccess","fileName",fileName);
}
}
Step 4:Validation
A simple validation for the uploaded file, display the error message if the uploaded file is empty.
Create FileUploadValidator.java file and put below code.
package com.bhagwan.common.validator;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import com.bhagwan.common.model.FileUpload;
public class FileUploadValidator implements Validator{
@Override
public boolean supports(Class clazz) {
//just validate the FileUpload instances
return FileUpload.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
FileUpload file = (FileUpload)target;
if(file.getFile().getSize()==0){
errors.rejectValue("file", "required.fileUpload");
}
}
}
File : message.properties
required.fileUpload = Please select a file!
Step 5: View Page(JSP pages)
We need to define the enctype=multipart/form-data attribute in the Springs form, else the upload file will not work well.
Create FileUploadForm.jsp and put below code.
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<style>
.error {
color: #ff0000;
}
.errorblock {
color: #000;
background-color: #ffEEEE;
border: 3px solid #ff0000;
padding: 8px;
margin: 16px;
}
</style>
</head>
<body>
<h2>Spring MVC file upload example</h2>
<form:form method="POST" commandName="fileUploadForm"
enctype="multipart/form-data">
<form:errors path="*" cssClass="errorblock" element="div" />
Please select a file to upload : <input type="file" name="file" />
<input type="submit" value="upload" />
<span><form:errors path="file" cssClass="error" />
</span>
</form:form>
</body>
</html>
If the file is uploaded successfully, display the uploaded file name.
FileUploadSuccess.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<body>
<h2>Spring MVC file upload example</h2>
FileName : "
<strong> ${fileName} </strong>" - Uploaded Successful.
</body>
</html>
Step 6: Spring Configuration
Register CommonsMultipartResolver to tell Spring to use commons-upload library to handle the file upload form. The rest is just normal bean declaration.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
<bean class="com.bhagwan.common.controller.FileUploadController">
<property name="formView" value="FileUploadForm" />
<property name="successView" value="FileUploadSuccess" />
<!-- Map a validator -->
<property name="validator">
<bean class="com.bhagwan.common.validator.FileUploadValidator" />
</property>
</bean>
<!-- Register the Customer.properties -->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="message" />
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
0 Comment(s)