Spring MVC Excel File Upload to Oracle Database Example

File Uploading is a very common task in any web application. Today we will learn about Spring File upload, specifically Spring MVC File Upload to upload Excel file and save its data into Oracle DB tables.

Spring uses MultipartResolver interface to handle the file uploads in web application. Below are two of the implementation :

  1. StandardServletMultipartResolver – Servlet 3.0 multipart request parsing.
  2. CommonsMultipartResolver – Classic commons-fileupload.jar

Tools used in this article :

  1. Spring 4.3.7.RELEASE
  2. Maven 3
  3. Tomcat 8.5

1. Project Structure

2. Project Dependency

First of all we need to add project dependency in our pom.xml

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”>
<modelVersion>4.0.0</modelVersion>
<groupId>com.learningsolo.spring</groupId>
<artifactId>springFileUpload</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>FileUpload</name>
<description>FileUpload</description>

<properties>
<jdk.version>1.8</jdk.version>
<spring.version>4.3.7.RELEASE</spring.version>
<jstl.version>1.2</jstl.version>
<servletapi.version>3.1.0</servletapi.version>
<logback.version>1.1.3</logback.version>
<jcl.slf4j.version>1.7.12</jcl.slf4j.version>
</properties>

<dependencies>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>

<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.9</version>
</dependency>

<!– compile only, deployed container will provide this –>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servletapi.version}</version>
<scope>provided</scope>
</dependency>

<!– Logging –>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${jcl.slf4j.version}</version>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>

<build>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>

<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>

</project>

3. Spring MVC configuration

To enable file upload we will add MultiPart bean in our spring mvc configuration file springDispatcherContext.xml  org.springframework.web.multipart.commons.CommonsMultipartResolver

Our final Spring configuration file looks like below.

springDispatcherContext.xml

 

Notice I am setting max upload file size for CommonsMultiPartResolver using “maxUploadSize”

4. Spring File Upload Controller Class

Controller class should have handler method to return view and also to process file upload. Controller class is responsible for mapping the request from url and executing it in the java layer.

FileUploadController.java

 

 

5. Spring View – upload.jsp

upload.jsp

 

6. Web.xml

web.xml

 

7. Service Layer

This layer will process excel file and read data from file and construct java object to save into DB. Here we have used apache POI to parse Excel data and read from excel sheet.

FileUploadService.java

 

8. DAO Layer to persist data into DB

DAO layer is responsible for persisting file data into DB tables in same format as in input file.

FileUploadDao.java

 

9. Sample Input Excel file

Below is the input excel file which is parsed and processed by our SpringFileUpload project. It read all the rows one by one and then process it and saves to oracle Database.

10. Spring File upload Result

Type below url in browser and open welcome page to upload file.

http://localhost:8080/springFileUpload/welcome

Upload success:

Download SpringFileUpload Project

Sushil Singh

My name is Sushil Kumar Singh. I’m the founder, designer and editor of this blog. I would love to hear your thoughts and opinions on my articles directly. You can write to me at sushilsingh@learningsolo.com. I’ll try to respond as soon as possible. Alternatively, you can connect with me directly on linkedin or github or Facebook.

Leave a Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of