Upload File with java and Spring Rest

Minakshi Jha
2 min readAug 10, 2021

--

As a developer we often come to a position where we need to create Uploaders and upload file via HTTP methods(APIs)

To create this feature, Spring Boot provides a very good Interface called MultipartFile.

MultipartFile requests break large files into smaller chunks which makes it efficient for file uploads.

Multipart can be used for any type of file upload such as text, image and csv.

If you see javadoc explanation, it says multipart upload will be written to a temporary location on disk or held in memory depending on the size of a file, such as file size less than 10240B will be held in memory otherwise it will temporarily move to disk until request gets completed.

After receiving the file it’s developer responsibility how they handle the file.

Another point to note, the default file size for upload is 1MB and this is configurable using below properties

spring.http.multipart.max-file-size=10MB
spring.http.multipart.max-request-size=11MB

Here in this case, I will be using a csv file for upload & parsing the CSV file to a model using library opencsv. (I will write another post about parsing csv file to java object).

To receive multipart file you just need @RequestParam(“file”) MultipartFile

You can find the code link below

okay, the final thing is, how we can test multipart request from postman?

To do this, select form-data as body option

Thereafter enter the file name that you have passed in you controller and select file as an option then import file in value field and you are good to go :)

Hope this helps you! Keep learning & growing!

--

--

Minakshi Jha

Full Stack Developer || Backend Engineer || Spring Boot || Java || Angular || Devops