Disable spring boot multipart upload by controller

2020-03-19 06:05发布

I am using spring boot for uploading files. The files sizes are usually about 2GB and we cannot use the default spring boot StandardServletMultipartResolver or CommonsMultipartResolver since the server have limited resource (disk space) or memory for buffering. So we would like to get the file inputsteam and store the file directly to the cloud storage.

I know spring boot has the multipart.enabled property so I can set it to false to skip the spring MultipartResolver. But this disables multipart globally. Does any one know if there is a way to disable multipart by controller/method?

2条回答
乱世女痞
2楼-- · 2020-03-19 06:58

If you enable resolve-lazily, the result is exactly what I think you're asking for.

spring.servlet.multipart.enabled = true
spring.servlet.multipart.resolve-lazily = true

Now you can write controllers with either form of signature.

Pre-parsing by the built-in multipart resolver...

@PostMapping("/upload1")
public ResponseEntity<Void> postUpload1(
    @RequestParam("metadata") MultipartFile metadata,
    @RequestParam("payload") MultipartFile payload)

Or post-parsing (which you can parse yourself)...

@PostMapping(path = "/upload2", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<Void> postUpload2(HttpServletRequest rawRequest)
查看更多
唯我独甜
3楼-- · 2020-03-19 06:58
登录 后发表回答