簡體   English   中英

Spring 引導分段文件上傳 - 提高性能的技巧

[英]Spring Boot Multipart File Upload - Tips to Improve Performance

我將RESTful API 暴露給用於將文件上傳到數據庫的reactjs前端應用程序。

服務器端 Controller 代碼:

@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();
}

客戶端JS代碼:

  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);
            });
  }

應用程序屬性

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

我正在嘗試上傳 20MB 文件(CSV 或任何其他文件),到達 controller 端需要太多時間。 (~ 1-2 分鍾)

請提出一些好的技術或技巧來提高使用相同的多部分請求的性能。 (例如:分塊或壓縮或流式傳輸)

我認為最簡單的方法是在 javascript 端僅將 zip 內容上傳給您 spring 引導應用程序。

使用它,您應該能夠在反應端獲取 zip 內容,並在您的 spring 應用程序中使用它。

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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM