简体   繁体   English

[org.springframework.web.multipart.support.MissingServletRequestPartException:所需的请求部分“图像”不存在]

[英][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:这是我的代码:

Spring boot弹簧靴

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

Flutter

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 配合得很好,知道如何解决这个问题吗?

Solution解决方案

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.

相关问题 Spring org.springframework.web.multipart.support.MissingServletRequestPartException,所需的请求部分“文件”不存在 - Spring org.springframework.web.multipart.support.MissingServletRequestPartException, Required request part 'file' is not present MissingServletRequestPartException:所需的请求部分“文件”不存在 Springboot - MissingServletRequestPartException: Required request part 'file' is not present Springboot 当前请求的类型不是[org.springframework.web.multipart.MultipartHttpServletRequest] - Current request is not of type [org.springframework.web.multipart.MultipartHttpServletRequest] org.springframework.web.multipart.MultipartException:当前请求不是多部分请求 spring boot - org.springframework.web.multipart.MultipartException: Current request is not a multipart request spring boot 如何修复 Spring 引导分段上传中的“所需请求部分不存在” - How to fix "Required request part is not present" in a Spring Boot multipart upload 多部分表单请求引发:不存在必需的MultipartFile参数&#39;image&#39; - Multipart form request throws: Required MultipartFile parameter 'image' is not present 无法将&#39;org.springframework.web.multipart.commons.CommonsMultipartFile&#39;类型的值转换为所需类型 - Failed to convert value of type 'org.springframework.web.multipart.commons.CommonsMultipartFile' to required type org.springframework.web.multipart.MultipartException - org.springframework.web.multipart.MultipartException 无法转换“org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile”类型的属性值 - Failed to convert property value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile' org.springframework.web.bind.MissingServletRequestParameterException:必需的日期参数“ startTime”不存在 - org.springframework.web.bind.MissingServletRequestParameterException: Required Date parameter 'startTime' is not present
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM