简体   繁体   中英

Spring Boot Multipart File Upload - Tips to Improve Performance

I am exposing RESTful API to the reactjs front end application which is used to upload a file to Database.

Server Side Controller Code:

@RequestMapping(value = "/api/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public UploadResponse uploadDocument(@RequestParam("doc") MultipartFile doc,
        @RequestParam("metaData") String metaData, HttpServletResponse response) {

    // logic to save in DB
    return new UploadResponse();
}

Client Side JS Code:

  uploadDocument(formData, callback) {
    instance.post('/api/upload', formData)
            .then((response) => {
              callback(response);
            })
            .catch((error) => {
              const errorObj = {
                status: error.response.status,
                data: {
                  message: error.response.data.message,
                },
              };
              callback(errorObj);
            });
  }

application.properties

spring.http.multipart.max-file-size=20MB
spring.http.multipart.max-request-size=20MB

I am trying to upload a 20MB file (CSV or any other), it is taking too much time to reach the controller side. (~ 1-2 minutes )

Please suggest some good techiniques or tips to improve the performance using same multipart request. (ex: Chunking or Compressing or Streaming)

I think the easiest way would be to just zip content at javascript side and upload it to you spring boot application.

Using this you should be able to zip content at react side and use it at your spring application.

Or you just zip at react side and upload the file in a normal way without any special octet stream handling in spring boot but just using java zip package classes to unzip files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM