Spring Mvc File upload With Example

Spring Mvc File upload With Example

In This Tutorial I Would like to explain how to upload file in spring.spring given multipart package org.springframework.web.multipart to support file upload handling.in spring file upload can be done by using MultipartResolver with the help of CommonsFileUpload.
To enable multipart handling we need to add CommonsMultipartResolver in spring configuration file like below
<bean id=multipartResolver"   class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- specify maximum file size in bytes -->
<property name ="maxUploadSize" value ="100000"/>
</bean>
It is very important that we need to commons-fileupload.jar to classpath.
So Lets See Complete Example
Project Structure





















Tools Used In this Article
JavadependenciesSetverToolsOperating System
Java 1.8Spring 4.3.2
commons-logging-1.2
commons-fileupload-1.3.2
commons-io-2.5
Tomcat 8.0.43Maven 4.0.0
Ecclipse Neon
Windows 7
Add dependencies to pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<groupId>springfileupload</groupId>
<artifactId>springfileupload</artifactId>
<version>0.0.1-SNAPSHOT</version>
war
<dependencies>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>

<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>

<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>

<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>



<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
</dependencies>

<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1&lt/;version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4&lt/;version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance"
xmlns=http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list><welcome-file>upload.jsp</welcome-file></welcome-file-list>
</web-app>
Now Write spring configuratio file i.e. Spring-servlet.xml
Spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
xmlns
=http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- MultipartResolver --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /> <!-- View Resolver --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/" /> <property name="suffix" value=".jsp" /> </bean> <mvc:annotation-driven /> <context:component-scan base-package="com.java2study" /> </beans>
Now Write controller class and annotate with @Controller
package com.java2study;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
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.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class FileUploadController {
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public ModelAndView fileUpload(@RequestParam("file1") MultipartFile file, HttpServletRequest request) {
String success = "";
if (!file.isEmpty()) {
try {
HttpSession httpSession = request.getSession();
ServletContext servletContext = httpSession.getServletContext();
String path = servletContext.getRealPath("/WEB-INF/upload");
FileOutputStream fileOutputStream = new FileOutputStream(path + "/" + file.getOriginalFilename());
fileOutputStream.write(file.getBytes());
success = " File : " + file.getOriginalFilename() + " uploaded Succesfully";
fileOutputStream.close();

} catch (IOException e) {
e.printStackTrace();
}
} else {
String error = "File is Empty!!!!";
}
if (success.length() > 0) {
return new ModelAndView("success", "message", success);
} else {
return new ModelAndView("error");
}

}

}
Now Write Jsp pages
upload.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<style type="text/css">
form {
border: 1px solid #cdcdcd;
width: 56%;
}
</style>
</head>
<body>
<form method="POST" action="upload" enctype="multipart/form-data">
File: <input type="file" name="file1"><br /> <input
type="submit" value="Upload"> click here to upload the file
</form>
</body>
</html>
success.jsp
<h1>${message}</h1>
error.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<h1>You have not choose any file to upload</h1>
</body>
</html>
output:




Download Source Code


Spring Mvc File Upload


-->