[英][org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'image' is not present]
I try to send an image with some attributes from flutter to spring boot endpoint, but spring boot not received the image at all and it gives me this error:我尝试将具有某些属性的图像从颤振发送到 Spring Boot 端点,但是 Spring Boot 根本没有收到该图像,它给了我这个错误:
Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'image' is not present]
here is my code:这是我的代码:
@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!');
}
});
My service work very well with Postman, any idea how can I solve this issue?我的服务与 Postman 配合得很好,知道如何解决这个问题吗?
Credit to this https://github.com/flutter/flutter/issues/37311#issuecomment-516967285归功于此https://github.com/flutter/flutter/issues/37311#issuecomment-516967285
I just add filename
and every thing work fine:我只是添加
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}');
请问你能告诉我们你是如何上传图片的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.