Spring Boot | File Upload and Download REST API | Store images in File System | JavaTechie

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to javateki in this tutorial we'll understand how to upload and download a file using file storage system okay all right so if you remember there are two approach to perform file upload and download scenario in approach one you can directly store your image to the database as a binary file in approach to you can store your image to any file system then you just need to set the path of that file to the database so both the approach is recommended based on the requirement so if you want more secure access and less frequently used image then you can directly store your image to the database but if you want faster access where the image is used frequently then you need to choose the second approach so if you remember we have completed approach one in our last tutorial don't worry if anyone not aware about it how to store image in database then i will share the link in video description you guys can check that out now let's focus on the store imagine file system so this is what we are going to demonstrate today now what basically we are going to do will store the image to the file system or you can consider any directory or folder then after add that particular file to the folder we'll get the path of it and then we'll store that path to the database this is what something called store image in file system now rather than store image into the database as a binary file will store somewhere in the file system okay so which will give you the faster access even we can check that out with our code so let's quickly demonstrate this approach with code so let's get started [Music] more [Music] so if you remember this is what the service we created storage service and if you go to the controller we have exposed to rest endpoint one to upload the image and want to download the image so this approach is something we directly store the image to the database and we retry from the database so let's test this out then in the same code we'll modify we'll use the file storage system okay so let's go to the postman this is what the url to save a image to the database i'll just add a file here just add any file let's say this guy now click on open send the request you can see here file uploaded successfully now let me copy this file name then i'll just do a gate call to download the image okay i'll change the url to the gate now send the request you can see here the image in your postman to get a better visibility i'll directly check in the browser this is what the image we uploaded now we can verify the same in our database as well now let me check select star from image data just run it just observe here this is what the image we just added right e2e dot png and that is what we are just rendering from the db now let's modify the same code to use the file storage system so i will not toss the existing code will create a separate entity and repo even i will create a separate method in the service to demonstrate the file storage system so let's create the entity i will just create an entity something like file data okay i will copy all the field from here only instead of byte array or binary file i will just store the path of a file to the database so let me copy everything then we'll just remove instead of this i'll just use string i will change it to the file path now even i don't want this also i will just copy other annotation to create a table you need to define entity and if you want to customize your table name you can use this annotation let me use file data okay and this is what all specific to the lombok so i believe you are aware about that so now since i created an entity i need to create a repository so i'll just create a interface with name file data repository extends it from jp repository and i will give the model name which is file data then data type of it which is integer okay now we have entity and we have repository so we'll just write a logic in our service so go to the service these are the two upload image to the db and download the image from the db so will not modify that existing code i'll write a separate method basically i will copy this then i can change it okay but before that first let me inject the um what is the repo we created file data repository okay private file data repository fine now let me add the method here upload image to file system okay so this is pretty clear guys there is nothing to confuse just store the image to the file or to the folder then get the path of that particular image with the folder and store it into the database now while retrieve get the path from the dv and retrieve it that is very simple okay so nothing to confuse i will explain step by step now what i need to add here instead of image data i just need to use file data this is what the new entity i created file data equal to i can directly use instead of this repo the repo which we injected file data repo right so what i'll do file data repo dot save what i need to save file data object okay so i can use file data dot builder because i just annotated address builder then next before i build i need to set all the field what all field i have in this entity id will be auto generated i have name type and file path so what i will do simply i can append all the fill the first field is name now i will get the name of the file from the file object which is multiple file i will send from the controller file dot get original file name now i will get the type of the file whether it is image or docs or text whatever that i can get the information from the file object get content type now next i need to set the file path but i don't know what is the file path right because i will not specify the file path i will add the binary file as part of the request the way we added here in the post request similarly i'll just add the file here but what is the path what is the file system so i need to create one folder where i want to store the all my images so i'll just create a folder let's say files or my files okay so i'll just get a path of it let me get a path of it this is what the folder path right let me copy this i will specify in the class label something like that i'll specify here private final string [Music] uh file path or something like that or i can specify folder folder path equal to this fine now in this particular folder this you can consider as my file system where i want to add my all the files so what i can do now i just need to generate a file name or file path so what i can do string this is what my file path now what will be my file path this and then the image name file dot get original file name this is what my file path so i can directly sorry i no need to change here i need to change the method which we wrote okay so this is what my file path and that particular object i want to store into the db okay so why there is error i need to add one more bracket fine now you can check that so if file data not equal to null file upload successfully where it uploaded you can give that information as well which is file path okay that's fine upload image to the file system so i repeat what i am doing here i just created a folder with name my files and in that particular folder i want to store the file okay so that is the reason i just specify the file path with the what i can say appropriate or valid path details okay now i just save the file path where i'm going to save my image in the dv so that is what i specify here now the next step you need to copy your file to that particular file system so how i can copy that file dot transfer to you can specify your file path okay fine the image which i will upload i just want to transfer that to the file path what i specify here this is what upload image to the file system now let's write the method to the download the image from the file system okay so i will copy the same method then i will just change it download image from file system okay so here i will just use the my repository which is file data repository i just need to create this method name there fine go to the service then it will give me the file data okay because we are just using our file storage system approach so why this is crying this will give okay this will give the file data fine now next what i need to do we need to get the file path so if you can check this code from the db itself we are getting the byte array which is our binary file and we are directly returning it back but now what you need to do first you need to get the file path so where your image is stored right then only we can perform or we can convert that to the byte array and we can return it back from our service so to get that i have the file details object right so i will change it to the file data or something like that so i can get it from file data because i am storing the path of it get or this is the optional right get dot get file path fine now i just need to convert this to the byte array or i will just use utility method of files that read all the bytes read all bytes from this file path and i'll give that path of it okay so i'll just simply specify a new file give the path of it and then dot to path it will throw the exception you can handle it or you can throw it it's up to you so this is also simple we are loading the first i am getting the path of the file which i store to the dv then i am just converting that to the byte array okay i'm just giving the path of it it will just convert to the byte array and i'm just returning it back from my service now let's move to the controller we are done with the service so i will just write these two method in controller so i will go to this class this is what my controller i'll copy this to method i'll just change the method signature fine upload image to file system now service i'll just call the method upload image to the file system that's it also i'll change the url because there is no possibility we can specify different url for same endpoint okay so i'll just change it to the something like folder or file system fine now similarly i need to change here download image from file system then here also i need to change the method name that's it it will throw the exception you can handle it or just throws it that's fine then here also i just need to change the url fine so everything is good now let me rerun our application okay so we just created two method to demonstrate using the file system store the image to the file system and retrieve the image from the file system but while retrieving if you observe first i am getting the image file data object from the db based on the file name because in the db i am storing the path of the file so i am getting that object here get file path then i am just converting it to the byte array and i am returning it back don't be confused guys okay so let's wait it to complete it started just go to the postman and just go to the post request okay just go to any post request i'll change the url because we are checking for file system okay now here i just need to add a file let me add something like it.png okay click on open now send the request okay there is something going wrong okay there is remove one additional slash send the request you can see here file uploaded successfully to this particular path and we are storing this path to the dv but before that let's verify whether really files this it.png file stored to this particular folder or not we are good it's storing right let me open it now just verifying the db let me refresh it i'll just fire a select query let me write down here select star from file data can you see here id is one and i am storing the path of my file which i stored in my folder i am not storing the binary file here this is what most important to understand okay and the name of the file type of the file now just retrieve it okay when we verify that in the directory or in the folder we have the file now let's retrieve it so to retrieve it what we can do or before that let me add few more file okay i will just use the post i will just add some other feed let's see javatekylogo.png send the request i'll just add other file as well let's say itflow.png send the request now let's verify we should have three file here right in my file folder this is what my file system where i want to store my all the images or files okay now let's retrieve it from the file system so to retrieve it let me copy any of the file name this is what the file name change it to the get and give the url or give the name of the file what you want to load this is gate request guys okay or better let me directly hit it in the browser make sure to verify the url i'm not retrieving it from dv i don't know whether i added this particular image to the db or not if you check the url slash file system you're loading the image from the file system just enter it you can see here you're getting the response back now what other images we added let me verify that so we did the post okay let me directly check in the db itself i will get the file name right it.png copy the name go to the browser just change the file name what you want to load from your file system because you will get the path from db and based on that path we are converting it to the byte array and we are returning it back see here we are able to render it similarly let's try for other one just go to the browser and just add the file name you can see here it's getting loaded fine so this is how you can perform file upload and download scenario either using database or either using file storage system so the real time use case for this file storage system if you check out the catalog page of amazon or flipkart you keep getting the images lot of images specific to the category what you will select right if they will store in the db and based on the user if they load it from the dv definitely it will take time to load the page so they will go for the file storage system similarly if you will check your facebook or insta they are also not storing the image to the dv they will also store the image from the file system so that you can in a fraction of second you can see the lot of feeds either in facebook or instagram okay so again it is recommended if you want faster access but if it is if you want to make it more secure and you want to access your image very less frequently then you can directly store image into the database that's all about this particular video guys thanks for watching this video meet you soon with a new concept
Info
Channel: Java Techie
Views: 14,648
Rating: undefined out of 5
Keywords: javatechie, file upload download rest api, spring boot, spring boot file upload and download
Id: 7L1BSy5pnGo
Channel Id: undefined
Length: 20min 27sec (1227 seconds)
Published: Tue Aug 30 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.