Last Updated on 02 July 2019   |   Print Email
This tutorial walks you through the steps of building an interesting Spring MVC web application that allows the user to upload files from her computer to the server. The files are stored in database using Hibernate. Here’s a screenshot of the file upload form:This sample application is built based on the combination of the following tutorials:
The above list is our recommendation, you can use newer versions. However you can use similar versions if you already installed the different ones. Now, let’s see how to build the database file upload application with Spring and Hibernate in step-by-step.
1. Creating Database
Because our application is for uploading files to database so we need to create a database and a table in MySQL server. Execute the following SQL script to create a database called filedb with a table called files_upload:
The controller class connects everything together as it specifies workflow of the application. Code the HomeController class with the following content:
Code of this controller class is pretty straightforward simple as well:
An implementation of the FileUploadDAO will be injected automatically (autowired) by Spring, as long as there is corresponding bean instantiation in the ApplicationContextConfig class.
The showUploadForm() method simply redirects the user to the upload page which is identified by the logical view name “Upload”.
The handleFileUpload() method handles submission from the upload page. It iterates through an array of upload files and save every file to the database via the fileUploadDao. And finally it redirects the user to a success page that simply displays a message telling the user that the file upload has been done.
Code of the Success.jsp page:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Upload Done</title>
</head>
<body>
<div align="center">
<h1>Files have been uploaded successfully!</h1>
</div>
</body>
</html>
8. Testing the Application
So far we have done all the coding part. Let’s test the application now. Make sure you deployed the application on Tomcat and the server is started. Type the following URL in your browser:http://localhost:8080/fileuploadThe upload page gets displayed as shown in the following screenshot:Choose two files from your computer and hit Upload. If everything is going fine, you would see the following page:And don’t forget to check the database to see if the files are actually saved:That’s it! You have built an interesting Spring-Hibernate web application for uploading files into database. For your convenience, we provide the sample project under the attachments section.
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Dear Nam, Good Evening.. I saw and understand your concept. But I need your help how to develop the project in my personal laptop or Desktop PC. I unable to get through.. Please tell me... how to develop real live Project by Upload etc.. I will remain highly obliged to you...
Comments
Thnaks.
What is the error/problem you got in your project?
Please be detailed so I can help.
Good Evening.. I saw and understand your concept. But I need your help how to develop the project in my personal laptop or Desktop PC. I unable to get through.. Please tell me... how to develop real live Project by Upload etc.. I will remain highly obliged to you...
Ayan