gpt4 book ai didi

java - 如何使用 Spring Boot 读取多部分?

转载 作者:行者123 更新时间:2023-11-30 05:39:42 26 4
gpt4 key购买 nike

我的 Controller 中有以下内容:

@PostMapping(value = "{storageId}/{repositoryId}", consumes = "multipart/form-data")
public ResponseEntity uploadViaPost(@PathVariable(name = "storageId") String storageId,
@PathVariable(name = "repositoryId") String repositoryId,
@RequestPart("content")
MultipartFile multipartFile,
HttpServletRequest request)
throws IOException
{
...
}

如果我想获取其余的多部分字段(不是文件),我需要使用哪些类/注释?有人可以提供一个如何处理其余字段的示例,例如:

----------------GHSKFJDLGDS7543FJKLFHRE75642756743254
Content-Disposition: form-data; name="platform"

UNKNOWN
----------------GHSKFJDLGDS7543FJKLFHRE75642756743254
Content-Disposition: form-data; name="version"

1.0
----------------GHSKFJDLGDS7543FJKLFHRE75642756743254
Content-Disposition: form-data; name="description"

UNKNOWN

最佳答案

您可以使用多个@RequestPart方法签名中的注释,并使用 required=false 标志注释所需的数据。

例如:

@PostMapping(path = "/test", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity testEndpoint(@RequestPart("file") MultipartFile file,
@RequestPart("version") String version,
@RequestPart(name = "platform", required = false) String platform) {
log.info("file_name = {}, version = {}, platform = {}", file.getOriginalFilename(), version, platform);
return ResponseEntity.ok().build();
}

和用于测试目的的curl:

curl -X POST \
http://localhost:8080/test \
-H 'Content-Type: multipart/form-data' \
-F file=@/path/to/file/test.txt \
-F version=1.0.0 \
-F platform=Test-Platform

关于java - 如何使用 Spring Boot 读取多部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55894007/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com