Spring MVC Java中不存在必需的多部分文件参数‘file’

omqzjyyz  于 2022-10-04  发布在  Spring
关注(0)|答案(1)|浏览(146)

控制器:

@RequestMapping(value = "/media", method = RequestMethod.POST, produces = "application/json", consumes = "multipart/form-data")
@ResponseBody
public MediaDto addMedia(@RequestParam(name = "file") MultipartFile file) throws IOException {
    LOG.info("Adding media file with name={}", file.getOriginalFilename());

配置

@Bean
public MultipartResolver multipartResolver() {
    CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
    commonsMultipartResolver.setMaxUploadSize(90000000);
    commonsMultipartResolver.setDefaultEncoding("UTF-8");
    return commonsMultipartResolver;
}

Angular

<input onchange="angular.element(this).scope().uploadFile(this)" id="chooseFile" name="file" type="file"
daolsyd0

daolsyd01#

输入标记中的id应该与控制器中的@RequestParam(name=“file”)name参数命名相同

相关问题