【问题标题】:how to pass a parameter in the Header with multipartFormData如何使用 multipartFormData 在 Header 中传递参数
【发布时间】:2022-11-12 18:03:31
【问题描述】:
    @PostMapping(value = "/uploadCV" , consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
    public ResponseEntity<?> uploadCV(@RequestHeader("token") String token,
                                      @RequestParam("file") MultipartFile cvFile) throws Exception {
        log.info(token + cvFile.getOriginalFilename()));
        return curriculumService.addCurriculum(token, pdfFile);
    }

如果我使用 MediaType.MULTIPART_FORM_DATA_VALUE 没有得到值“令牌”: 2022-11-11 16:13:28.040 WARN 7283 --- [nio-8080-exec-4] .w.s.m.s.DefaultHandlerExceptionResolver:已解决 [org.springframework.web.bind.MissingRequestHeaderException:方法参数类型所需的请求标头“令牌”字符串不存在]

如果我使用 MediaType.MULTIPART_MIXED_VALUE 不获取文件: org.apache.tomcat.util.http.fileupload.FileUploadException:请求被拒绝,因为没有找到多部分边界

我能怎么做?

【问题讨论】:

    标签: spring spring-boot spring-mvc microservices


    【解决方案1】:

    您使用MediaType.MULTIPART_FORM_DATA_VALUE 编写的代码绝对正确。从错误消息中很清楚问题是什么。

    2022-11-11 16:13:28.040 WARN 7283 --- [nio-8080-exec-4] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MissingRequestHeaderException: Required request header 'token' for method parameter type String is not present]
    

    问题:

    代码需要带有键的标题名称令牌应该出现在请求中,但它不存在。

    解决方案:

    请添加带有键的标题名称令牌像这样再试一次。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-21
      • 2020-05-25
      • 2017-11-23
      • 2018-08-05
      相关资源
      最近更新 更多