获取HTTP状态400 – 必需的MultipartFile参数’file’在spring中不存在

我正在尝试使用spring上传文件。 下面是我的代码我是如何工作的,但如果我尝试使用它,我得到这个response

HTTP状态400 – 不存在所需的MultipartFile参数“file”

我不明白错误是什么。

我正在使用高级rest客户端进行测试,我正在上传文件作为附件。

我的Javacode:

 @RequestMapping(value = "/upload",headers = "Content-Type=multipart/form-data", method = RequestMethod.POST) @ResponseBody public String upload(@RequestParam("file") MultipartFile file) { String name= "test.xlsx"; if (!file.isEmpty()) { try { byte[] bytes = file.getBytes(); BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name))); stream.write(bytes); stream.close(); return "You successfully uploaded " + name + "!"; } catch (Exception e) { return "You failed to upload " + name + " => " + e.getMessage(); } } else { return "You failed to upload " + name + " because the file was empty."; } } 

spring需要

bean来处理文件上传。

您应该在application context文件中注册此bean。

Content-Type也应该有效。 在你的情况下enctype="multipart/form-data"

EDIT1:

您可以将上传和内存大小赋予bean属性:

          

当你提前选择文件rest客户端时, 右边有一个输入框 ,写入参数的输入框名称,在你的情况下参数的名称是文件

控制器@RequestParam(“file”)中定义的参数名称

在此处输入图像描述

当我在Krajee bootstrap inputfile示例中已经很好地配置了bean id之后,我在参数“file”的输入框名称中写入后,它对我有用(“ https://github.com/kartik-v/bootstrap-fileinput “)。