java - Spring Boot,在 REST 控制器中上传任意数量的文件
问题描述
我想创建一个端点,它接受来自用户的任意数量的不同文件。
例如:
然后,我想在我的控制器中接收它作为Map<String, FilePart>
(或任何其他结构,我可以从中知道哪个文件是哪个):
{
"file1": "cactus-logo.png",
"file2": "logo.png",
"file3": "logo.png" (this one is actually different than file2 but has the same name)
}
我尝试了一些组合@RequestPart
......
当我做:
@RequestPart Map<String, FilePart> files
或者
@RequestPart MultiValueMap<String, FilePart> files
我越来越:
org.springframework.web.server.ServerWebInputException:400 BAD_REQUEST“所需的请求部分'文件'不存在”
当我做:
@RequestPart("files") List<FilePart> files
我需要提交这样的文件:
然后我不知道哪个文件是哪个文件(如果它们具有相同的名称):
最后,我可以这样做:
@RequestPart("file1") FilePart file1, @RequestPart("file2") FilePart file2, @RequestPart("file3") FilePart file3
然后它按预期工作,但这样一来,就可以始终只提交 3 个文件。我想提交任意数量的文件。
正如评论中所建议的:
@PutMapping(value = "/{component}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public void upload( @RequestParam Map<String, MultipartFile> files ) throws IOException {
并且地图总是空的:
解决方案
推荐阅读
- ios - PDFView 未调整大小
- apache - 无法将 https 非 www 重定向到 Apache 上的 www
- android - 如何制作通用类型转换函数以将上下文转换为任何活动?
- python - 找不到文件 python.h
- html - 如何比较 Angular 中的 ENUMS?
- javascript - react.js 通过异常发布字符串或 json
- reactjs - 无法使用 react-router 手动刷新或写入 url
- rust - Rust 中函数前的括号是什么?
- javascript - 从 Javascript 中的 Web 源读取然后使用数组
- bash - SSH 配置文件限制为 100 个身份文件