Spring Boot,在REST控制器中上传任意数量的文件。

huangapple go评论75阅读模式
英文:

Spring Boot, upload any amount of files in REST controller

问题

我想创建一个端点,接受用户上传的任意数量的不同文件。

例如:

Spring Boot,在REST控制器中上传任意数量的文件。

然后,我想在我的控制器中将其作为 Map<String, FilePart>(或者从中我可以知道哪个文件是哪个的任何其他结构)来接收:

{
    "file1": "cactus-logo.png",
    "file2": "logo.png",
    "file3": "logo.png"(这一个实际上与file2不同,但文件名相同)
}

我尝试过一些@RequestPart的组合...

  1. 当我使用:
@RequestPart Map<String, FilePart> files

或者

@RequestPart MultiValueMap<String, FilePart> files

我得到:

org.springframework.web.server.ServerWebInputException: 400 BAD_REQUEST "Required request part 'files' is not present"

  1. 当我使用:
@RequestPart("files") List<FilePart> files

我需要像这样提交文件:

Spring Boot,在REST控制器中上传任意数量的文件。

然后我无法知道哪个文件是哪个(如果它们具有相同的名称):

Spring Boot,在REST控制器中上传任意数量的文件。

  1. 最后,我可以这样做:
@RequestPart("file1") FilePart file1,
@RequestPart("file2") FilePart file2,
@RequestPart("file3") FilePart file3

然后它按预期工作,但是这样只能始终提交3个文件。我想提交任意数量的文件。

  1. 如评论中所建议的:
@PutMapping(value = "/{component}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public void upload(
     @RequestParam Map<String, MultipartFile> files
) throws IOException {

而且这个映射始终为空:

Spring Boot,在REST控制器中上传任意数量的文件。

Spring Boot,在REST控制器中上传任意数量的文件。

英文:

I'd like to create an endpoint which accepts any amount od different files from user.

For example:

Spring Boot,在REST控制器中上传任意数量的文件。

Then, I'd like to receive it in my controller as a Map&lt;String, FilePart&gt; (or any other structure from which I'll know which file is which):

{
    &quot;file1&quot;: &quot;cactus-logo.png&quot;,
    &quot;file2&quot;: &quot;logo.png&quot;,
    &quot;file3&quot;: &quot;logo.png&quot; (this one is actually different than file2 but has the same name)
}



I tried some combinations of @RequestPart...

  1. When I do:

    @RequestPart Map&lt;String, FilePart&gt; files
    

or

    @RequestPart MultiValueMap&lt;String, FilePart&gt; files

I'm getting:

> org.springframework.web.server.ServerWebInputException: 400 BAD_REQUEST "Required request part 'files' is not present"

  1. When I do:

    @RequestPart(&quot;files&quot;) List&lt;FilePart&gt; files
    

I need to submit files like that:

Spring Boot,在REST控制器中上传任意数量的文件。

And then I don't have the information which file is which (if they have the same name):

Spring Boot,在REST控制器中上传任意数量的文件。

  1. Finally, I can do:

         @RequestPart(&quot;file1&quot;) FilePart file1,
         @RequestPart(&quot;file2&quot;) FilePart file2,
         @RequestPart(&quot;file3&quot;) FilePart file3
    

And then it works as expected, but with that, it's possible to submit always only 3 files. I'd like to submit any number of files.

  1. As suggested in comments:

         @PutMapping(value = &quot;/{component}&quot;, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
         public void upload(
              @RequestParam Map&lt;String, MultipartFile&gt; files
         ) throws IOException {
    

and the map is always empty:

Spring Boot,在REST控制器中上传任意数量的文件。

Spring Boot,在REST控制器中上传任意数量的文件。

答案1

得分: 2

Spring Boot,在REST控制器中上传任意数量的文件。你应该使用@RequestParam

@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public void upload(@RequestParam Map<String, MultipartFile> body) {

}

然后通过表单数据媒体进行发布:

file1: 选择文件
file2: 选择文件
file3: 选择文件
....
英文:

Spring Boot,在REST控制器中上传任意数量的文件。You should use @RequestParam

@PostMapping(value = &quot;/upload&quot;, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public void upload(@RequestParam Map&lt;String, MultipartFile&gt; body) {

}

Then post via form-data media ie:

file1: select file
file2: select file
file3: select file
....

答案2

得分: 0

我没有提到我在使用响应式的 WebFlux,因此 @Toàn Nguyễn Hải 提供的解决方案在我的情况下不适用。我相信它适用于非响应式的应用程序。

对于 WebFlux,以下方法有效:

Spring Boot,在REST控制器中上传任意数量的文件。

公平地说,我不会接受任何答案,因为它们都可以。谢谢!

英文:

I did not mention I'm using reactive webflux, thus solution posted by @Toàn Nguyễn Hải does not work in my case. I believe it works in non-reactive applications.

For web flux, the following works:

Spring Boot,在REST控制器中上传任意数量的文件。

To be fair, I won't accept any answer, because both of them are fine.

Thanks!

huangapple
  • 本文由 发表于 2020年9月13日 16:06:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63868546.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定