Java 如何使用Sring引导读取多部分?
我的控制器中有以下各项:Java 如何使用Sring引导读取多部分?,java,spring,multipartform-data,Java,Spring,Multipartform Data,我的控制器中有以下各项: @PostMapping(value = "{storageId}/{repositoryId}", consumes = "multipart/form-data") public ResponseEntity uploadViaPost(@PathVariable(name = "storageId") String storageId, @PathVariable(name = "reposi
@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
您可以在方法签名中使用多个注释,并使用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 -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
哦,对了!我不知道你可以在任何对象上使用注释。谢谢!:)我觉得没用。我得到了这些的
null
-s。很有趣。。。如果我将它们替换为@RequestParam
,它们就可以正常工作。我原以为,作为多部分内容,它们需要进行不同的注释。