Spring - How to stream large multipart file uploads to database without storing on local file system

后端 未结 2 827
情书的邮戳
情书的邮戳 2020-12-05 14:44

Spring boot\'s default MultiPartResolver interface handles the uploading of multipart files by storing them on the local file system. Before the controller method is entered

2条回答
  •  离开以前
    2020-12-05 15:29

    You could use apache directly, as described here https://commons.apache.org/proper/commons-fileupload/streaming.html.

    @Controller
    public class UploadController {
    
        @RequestMapping("/upload")
        public String upload(HttpServletRequest request) throws IOException, FileUploadException {
    
            ServletFileUpload upload = new ServletFileUpload();
    
            FileItemIterator iterator = upload.getItemIterator(request);
            while (iterator.hasNext()) {
                FileItemStream item = iterator.next();
    
                if (!item.isFormField()) {
                    InputStream inputStream = item.openStream();
                    //...
                }
            }
        }
    }
    

    Make sure to disable springs multipart resolving mechanism.

    application.yml:

    spring:
       http:
          multipart:
             enabled: false
    

提交回复
热议问题