[org.springframework.web.multipart.support.MissingServletRequestPartException: required request part



我尝试将具有一些属性的图像从扑动发送到春季启动端点,但春季启动根本没有收到图像,它给了我这个错误:

Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'image' is not present]
下面是我的代码:

春季启动

@PostMapping("")
public ResponseEntity<UsersEntity> createNewUser(@RequestParam(value = "image") MultipartFile image, UsersEntity user) {
UsersEntity response = userService.createUser(user, image);
return ResponseEntity.ok(response);
}

摆动
var postUri = Uri.parse("http://localhost:8080/v1");
var request = new http.MultipartRequest("POST", postUri);
request.fields['fName'] = firstNameController.text;
request.fields['lName'] = lastNameController.text;
if(image != null) {
request.files.add(http.MultipartFile.fromBytes(
'image', image!, contentType: MediaType.parse('multipart/form-data')));
}
Map<String, String> headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Headers": "Origin,Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,locale",
"content-type": "application/json"};
request.headers.addAll(headers);
request.send().then((response) {
if (response.statusCode == 200) {
print("Success!");
} else {
print('Error!');
}
});

我的服务与邮差工作得很好,你知道我该如何解决这个问题吗?

解决方案

归功于此https://github.com/flutter/flutter/issues/37311#issuecomment-516967285

我只是添加了filename,一切都很好:

request.files.add(http.MultipartFile.fromBytes(
'image', image!, 
contentType: MediaType.parse('multipart/form-data'), 
filename: 'test.jpg'));
var uri = Uri.parse('$baseUrl/services/selfemployment/check-confirmation-code');
var request = new http.MultipartRequest("POST", uri);
request.headers.addAll(baseHeader);
request.fields['example'] = example;
request.fields['image'] = image;
http.Response response = await http.Response.fromStream(await request.send());
print('Uploaded! ${response.body} ++ ${response.statusCode}');

相关内容

  • 没有找到相关文章

最新更新