Springs

Spring MVC Excel File Upload to Oracle Database Example

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

 

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 Sample from GitHub.com

 

9 thoughts on “Spring MVC Excel File Upload to Oracle Database Example

  • where is basedao,Where is ExcelTemplatev? In the Above Example?

    Reply
  • why i keep getting request method ‘post’ not supported

    Reply
    • Sushil Singh

      Can you please elaborate more on your issue? Please give more details on the issue that you are facing.

      Reply
  • I have downloaded the project but am not able to run it. can you please help me to get the result.

    Reply
    • Sushil Singh

      You should import this project as Existing maven Project into your eclipse and add a tomcat server to run on.

      Reply
    • Sushil Singh

      What is the error you are getting?

      Reply

Leave a Reply

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