Java: Uploading and Downloading Files from the Database using Spring Boot | ChargeAhead LLC | Skillshare

Java: Uploading and Downloading Files from the Database using Spring Boot

ChargeAhead LLC, Technologist

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
10 Lessons (15m)
    • 1. Overview

    • 2. Audience

    • 3. Tools

    • 4. Prerequisites

    • 5. Creating the Spring Boot Project with Dependencies

    • 6. Creating Model And Repository Layers

    • 7. Creating the Service Layer

    • 8. Creating the Controller Layer

    • 9. Creating the View layer using Thymeleaf

    • 10. Summary


About This Class

In this course we will create a Spring MVC project using Spring Boot and demonstrate how to upload and download documents to and from a backend H2 database. You can swap the database with another one like MySQL, Oracle etc easily adding relevant dependency and properties.


1. Overview: often times we want to store files and images in the database and then later to retrieve them for end users to see or download. These fights in this course will start off talking about the audience pressure visits and tools needed for the scores. They will use spring boot to create a new project, including dependencies, to enable us to easily read and write toward it. A vase. We will create the model service and controller layers. We will implement the view lier using time leaf. Here is how our app is going to look. We should be able to provide a simple interface to our end users to upload files toward it away's and then download these files from the database to All right, let's get started. 2. Audience: this course would be suitable for Jammeh, the Clippers and soft. For our critics, this is a course suitable for even someone getting started as I will be taking you step by step through the entire process. It should not be difficult to follow along. And this court will also be useful for experience Java programmers as they will get exposure to some new tools and methods to use in. There's our programs. 3. Tools: we will have a lot of demos in the scores as a believe that is the best way to learn the tools I would using. The scores are off course. Jama. I would be using open Judy Kay Warren, 11 but you can feel free to use Java or open Judy Kay versions eight or above, and most of examples should work for the i. D. E. I would be using Sprinkle Sweet, which is a flavor of eclipse but is highly customized and optimized for spring development . I would be using the version for it is open source and free and comes with a lot of bells and whistles. But I think you would like, however, if your family with and we like to use some other I d like intelligent etcetera, please feel free to do so, for the database says we will be using H two and open source and memory did a vase. You can find the gold for the demos in the scores and the falling good. Have you Earl 4. Prerequisites: to get the most out of the scores. It will be good to have a basic and it's turning off Java. I will be using me even as the bill tool, so some understanding off maven would be useful. However, I will be using the spring tools Feet I D E, which would do a lot of heavy lifting for us and create the basic structure and artifacts, and so should be easy to follow along, even though you do not have a lot of exposure. 5. Creating the Spring Boot Project with Dependencies: I have sprinkled speed running on my machine, which is a flavor of eclipse. Let's click file new other and then from under Spring boot Less Jews Spring Starter Project . Let's give the name to our project as blob demo. You will use maven. Let's include a spring boot deaf tools. It is optional, and it helps in the development process by not home to restart her application to see our changes. Let's include spring did a J P A Spring Web and time leaf click finish for this demo will use the H two. Did a vase, A lightweight open source. John did a vase. Let's open the bomb dot xml file and include the H two. Different and see which will left spring boot or to configure this database for us with just a few properties. Let's now open the application or properties file is to has a Web console. Let's enable it by sitting spring dot each to darken Soul dot enabled property to true. Let's specify the spring data source. You are I, which is a file based on the base pointing to the Dev hyphen DB file. Let's put the database driver class name as are not each student driver now for the simplicity of the demo level. Led GOP, a auto Create the tables for us upon startup. So let's put this print georgie ph dot hibernate did yelled at auto to create. Let's customize the server port as 8777 Now for the file upload. Let's specify the spring got servility toward multi part, not enable to. True, Let's specify the multipart file sized reassured as two kilobytes. Let's set the multipart max file size as 200 megabytes, said the Max request size. Also 200 megabytes. You're gonna just these values too soon. The file size you want to allow to upload. All right, now, let's create our database table toe, hold the files. 6. Creating Model And Repository Layers: here is our main class. So right click on the project Jews New package Call it model now, right, click and choose new class and call it Doc. Let's annotated as an entity toe work with G P A. With the active alert notation. Let's name it of its table as docks, it's fixed the import. First, let's create a primary key with the ad I. D. Notation. Let's have a generated automatically for us using the Arjun rigid value with strategy as generation type got identity. This I D column will be off type integer with the name I D. Let's create a string variable to hold the document. Name another to hold its type with the L. A. B or large object notation. We define the bite array with name data fixed the imports. Let's create a no arcs constructor, right click and choose. Generate constructor. Using feels select everything except I D as it will be. Ah, Trojan Richard for us, right click and choose source. Generate genders and sitter's select all engine rate. All right now, let's create a package for the repository, but right clicking and choosing new package and calling it repository directly on the package. Jews New interface and call it Dark repository. Click Finish. Let's Extended with the GP repository working on Object Dog, which hasn't ended yet. I d Let's fix the import and Spring GP will do the heavy lifting for us. Creating the Kurd matters for us without us even writing a single line of code. 7. Creating the Service Layer: next, let's create a service package by right clicking and choosing new package and calling it service. Let's create a new class by right clicking and choosing new class and calling it dark storage service. Click finish. Let's give it the ax service. In addition, lets out a wire our doc repository. Fix the imports. Let's was create a method to save the multipart file past to the database. We get the document name by calling file. Don't get original file name. Let me create a try catch block. Let's create a new document instance by using the constructor passing in the document name Document type as find out, get content type and the data as filed or get bite. Let's don't say the document to the table by calling Doc Repository. Don't save passing in the dark and then returned the resulting doc. Fix the import. If there is an exception, we were done. No, no. Let's create a method. Richard owns a document met her name is Get file, which takes in the indigent i d of the document we want to retrieve. Let's return dark repository dot Find by I d. Passing in the file. I d to account for the case for file on phone, we make the return document optional. To get all files, we can create a matter which don't a list of documents called get files. Richard turns dark repository. Don't find all the depository. Makes it so easy for us to implement the database crowd functionality. 8. Creating the Controller Layer: Now let's create a controller package but right clicking and choosing new package and calling it controller. Let's create a new class, but our clicking and choosing new class. Call it dark controller. That's market with the AK controller Notation lets out a wire the dog store IT service. Fix the imports. Let's create a get mapping mapping to the do TRL. The method returns a string, which is the name off the time. Leave template display and take the model as the input parameter. We got a list of documents calling the docks toilet service, not get files method added to the model as an attribute with the name dogs. Then we're done. The name of the time. Leave template dark. Virtual creates soon that's fixed the imports. Next, let's create a post mapping for you. Earl upload files. The method returns a string and takes in the request parameter, which is an array of multipart files inside the loop over the multipart files and call the dog store it service. Not safe file mattered to save it little database. Then we return. Redirect colon Slash setting it back to the Rue du Orel to display the documents in the main page. Next we created Get mapping off Download slash Specifying the file early sequestered. The matter will return Response Entity off resource. We name it. Download file. We get the part variable in an integer variable file I d inside we get the document. Were calling doc store it service. Don't get filed passing in the file i d Let's fix the imports. We return response and really don't okay Specify the content type as media type dot parts media type. Getting it from doc dot get doc type we after the response header contract disposition as attachment with the filing as dark dot get document name and then in the body was sent a new bite every resource off. Doc, don't get data. Let's fix the name. Let's change the return value to response into the off Valtteri resource. That is it. Now let's create our time. Leave template 9. Creating the View layer using Thymeleaf: time leave expects the templates in this source. Main Resources templates for her directly there and choose new Web html file. Call it Dr HTML. The same name. We're turning another controller. Let's arm the time. Leave name space, the title in the body. Let's create a Dave to upload multiple files. Let's create a form with I D upload files. NEMA profiles Method Posed an action as time leaves teach action as act curly braces and then slash upload files. No, the time leave syntax is first body, including type as multipart former leader. Next we create input type has filed Name is files the same name our control is expecting. If you want multiple files, upload functionality. Put multiple. Otherwise, don't put multiple. If you just formed a single file, a Plourde make it required. Next, let's create a summit button. Now let's critter Dave to display and download uploaded files. Let's create a table table head element off idly name. Donald Link. Next, let's create a table body using time leaves th each we treat over the documents returned back as using dollar docks model. Attribute. Taking in Egypt. Trish In the document Doc Variable That insight td Using th Colin text. Forget the document i d as dollar dark dot i d No dissent ICS Dr Dark Name for the document name. For the download link, we create a teach calling hlf element specifying the download link as curly brace slash download file slash and then on the daughter i d. As the path variable. Close the curly brace Display down Lord their dessert. We have the page ready. We have already tired to the issue database in our application or properties. Oh, let me fix the kissing off the driver Name. Good. Let's run our app. Right Click Choose room as spring boot app. It starts on port 8777 Let's go to the browser and go to local host calling 8777 slash ish to happen. Console to see your issue Database The jury Bcu Earl is delicacy Ural from or application or properties file. Pointing to our file based E B s Is the deformities or name? No password required. Click connect Here we see our dogs table order created. Let's run select star from dogs. There are no rules currently. Next let's look at our website hairs over page. Let's could be bro's button. I didn't pick a war document a Pdf document and an Excel file The shovel ready, Click submit, and they're uploaded. The direct brings back to the page, refreshing. The document lists from the Red Abi's Here are the download links going to issue Database console. Different equerry again We see where Documents the document name except Ra. Let's click down Lord on the Doctor. Get it from the database. Let's open it. I am on over to the next, and so it will open it with liver Office opens just fine now, nor the beauty F and open it. I don't know the Excel and open it cool, so we were able to successfully upload and download documents to our database. 10. Summary: in this course, we created a spring embassy project using spring Judah GP a tow APL ore and download documents to a back and Eastern rid of ways. We created a Time live page to demonstrate the upload and download functionality. Thanks for watching.