gpt4 book ai didi

java - 在 Spring boot 和 REST API 中上传 multipart/form-data 文件

转载 作者:行者123 更新时间:2023-11-30 06:08:18 28 4
gpt4 key购买 nike

我想上传一个 .png 文件并存储该文件。当我使用 content-type=multipart/form-data 发送它时,我收到此异常

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 
'application/octet-stream' not supported .

我认为我的应用程序不理解表单数据 contentType 并引发不相关的异常。怎么了?

    @Controller
public class ProfileController {
private static final Logger logger = LoggerFactory.getLogger(ProfileController.class);

@Autowired
ProfileService service;

//http : http --pretty all --form PUT localhost:8080/profile/v1?thumb=glb
@PutMapping(value = "/v1")
public Mono<ResponseEntity<String>> upload(@RequestPart(value = "thumb", required = false) String thumbFilterName,
@RequestPart(value = "genthumb", required = false) String genthumb,
@RequestPart("hash") String hash,
@RequestPart("uploader") Long uploader,
@RequestPart("file") MultipartFile file) throws Exception {


System.out.println(thumbFilterName);
System.out.println(gentumb);
System.out.println(uploader);
System.out.println(file);
//and doing storage stuff!
return Mono.just(ResponseEntity.ok().build());
}
}

这是我的 Rest 客户端,用于发送文件。注意回复。

这些是restClient的屏幕截图。使用restClient发送文件:

sending file with restClient

这是设置标题

and this is setting headers

最佳答案

uploader中,您必须将@RequestPart更改为@RequestParam

@RequestPart("uploader") Long uploader,

Javadoc explaination :

Note that @RequestParam annotation can also be used to associate the part of a "multipart/form-data" request with a method argument supporting the same method argument types. The main difference is that when the method argument is not a String, @RequestParam relies on type conversion via a registered Converter or PropertyEditor while @RequestPart relies on HttpMessageConverters taking into consideration the 'Content-Type' header of the request part. @RequestParam is likely to be used with name-value form fields while @RequestPart is likely to be used with parts containing more complex content (e.g. JSON, XML).

关于java - 在 Spring boot 和 REST API 中上传 multipart/form-data 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50817828/

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