简体   繁体   English

Spring 引导分段文件上传 - 提高性能的技巧

[英]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.我将RESTful API 暴露给用于将文件上传到数据库的reactjs前端应用程序。

Server Side Controller Code:服务器端 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();
}

Client Side JS Code:客户端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);
            });
  }

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.我正在尝试上传 20MB 文件(CSV 或任何其他文件),到达 controller 端需要太多时间。 (~ 1-2 minutes ) (~ 1-2 分钟)

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.我认为最简单的方法是在 javascript 端仅将 zip 内容上传给您 spring 引导应用程序。

Using this you should be able to zip content at react side and use it at your spring application.使用它,您应该能够在反应端获取 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. 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