代码之家  ›  专栏  ›  技术社区  ›  Kalle Richter

如何将这个mutlipart文件上传到springboot2.4?

  •  0
  • Kalle Richter  · 技术社区  · 4 年前

    val mvcResultImage = this.mockMvc!!.perform(MockMvcRequestBuilders.multipart("/somepath)
            .file("files[]", imageFile.getBytes())
            .characterEncoding("UTF-8"))
            .andReturn()
    

    在一个具有功能的控制器的集成测试中

    @PostMapping(path = ["/somepath"],
        consumes = [MediaType.MULTIPART_FORM_DATA_VALUE],
        produces = [MediaType.APPLICATION_JSON_VALUE])
    @ResponseBody
    fun createFromBytes(@RequestParam("files[]") file: MultipartFile): ResponseEntity<Any> {
       ...
    }
    

    在2.3中,我能够在控制器函数中处理请求,而在2.4中,控制器函数会引发 org.springframework.web.multipart.support.MissingServletRequestPartException 带着信息 Required request part 'files[]' is not present

    我在迁移指南和此版本更改的已处理问题列表中找不到任何内容。

    重命名为 file 在这两个控制器和请求没有帮助,我不记得为什么我添加了 []

    我通过maven父机制使用springboot spring-boot-starter-parent:2.4.1 .

    0 回复  |  直到 4 年前
        1
  •  0
  •   Kalle Richter    4 年前

    这是 a known issues in Spring Boot MockMultipartFile MockMultipartFile( String name, @Nullable String originalFilename, @Nullable String contentType, @Nullable byte[] content) (产品规格) originalFilename 问题)。