Which one is better for Creating an JAVA Springboot Rest Endpoint to upload file - Multipart file vs base 64 encoded byte array in the POST body

Viewed 611

I am trying to create an application, where documents are uploaded from the Angular application and send to the Spring Rest Endpoint, which will save the document in Database. I could see two options

  1. Creating an endpoint which accepts base encoded array of bytes in the body of the POST request.
  2. Creating the endpoint to accept Multipart file

Which one is better in terms of performance and why ? Please note, my document size can be from a couple of MBs to 25 MB.

1 Answers

Base64 is a way to encode binary data into an ASCII character format by translating it into a radix-64 representation. I recommend you that never use Base64 for large file/data upload to the server because it converts whole data and posts it to the server.

An the other hand, Multipart is a way to upload the file to a server in the form of parts which are in bytes. Multipart/form-data is applied to a form though, so you can send everything in a multi-part form, including "regular" data also.

So, I think this can be useful for you:

@PostMapping("/api/update")
public ResponseEntity upload(@NotNull @NotEmpty @RequestParam("file") MultipartFile file) {}
Related