Issue
I am building a REST API with Spring boot to allow applicant to fill out a form as below:
- fullName
- socialsecurityNumber
- birthDate
- picture(photo)
- resume(file)
I am using MSSQL Server as database, What is the best way to save a file in a Spring boot Application? I mean, the resume and the photo of the applicant. Should i save it as a blob in the database or use a link on a string field to save the path? What is the pros and Cons of the both altenatives.
Solution
It depends on the data size.
In general, Saving a file on the file system is much better as compare to stroring it in the database. You should prefer to store only paths in the database and your application should use those paths to retrive the files from the file system.
There are certain problems when it come to storing files in the databases.
- You can only store file up to certain size in the database due to size restriction.
- Database size will grow at much faster rate so does the backup size.
- Restoration will also take longer time.
- Inlcusion of file data means increase in the size of query payload, making such queries slower simply because there is more data to be transferred between the application and the database.
- In term of storage cost, database is more expensive.
As per your requirement, you're going to store image & resume file (won't be a large file) so you can also opt for storing the content in the database.
Answered By - harry
Answer Checked By - Katrina (JavaFixing Volunteer)