禁用控制器的Spring

禁用控制器的Spring

本文介绍了禁用控制器的Spring Boot分段上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Boot来上传文件.文件大小通常约为2GB,我们不能使用默认的Spring Boot StandardServletMultipartResolverCommonsMultipartResolver,因为服务器的资源(磁盘空间)或用于缓冲的内存有限.因此,我们希望获得文件输入团队并将文件直接存储到云存储中.

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.

我知道spring boot具有multipart.enabled属性,因此我可以将其设置为false以跳过spring MultipartResolver.但这会全局禁用多部分功能.有谁知道是否有一种方法可以通过控制器/方法禁用多部分功能?

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?

推荐答案

如果启用resolve-lazily,则结果恰好是我认为的要求.

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)

这篇关于禁用控制器的Spring Boot分段上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 06:59