必需的多部分文件参数"file"在 Spring 4.3.1 中不存在



我正在使用 spring 4.3.1,我将使用 ng-file-upload 库上传文件。 这是我的JavaScript代码,当我将JavaScript代码连接到PHP服务器时,它运行良好。

var promise = Upload.upload({
url: url + "upload",
method: 'POST',
file: file,
ignoreLoadingBar: true
}).success(function(response) {
flatForm.jsonForm = response.jsonForm;
flatForm.xmlForm = response.xmlForm;
}).error(function(response) {
$rootScope.$broadcast('veil:hide', {});
});

我在/web-inf/lib 文件夹中附加了 commons-io-2.4.0.0.jar 和 commons-fileupload-1.3.1.jar。 e 我在applicationContext.xml文件中添加了多部分解析器。

<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1000000000" />
</bean>

这是我的控制器类。

@ResponseBody
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public void upload(@RequestParam("file") MultipartFile file) throws Exception {
if (file == null || file.isEmpty()) {
throw new Exception("No file was sent.");
}
}

但是当我上传文件时,我收到这样的错误。

Required MultipartFile parameter 'file' is not present

我该如何解决这个问题? 请帮助我。 感谢您的观看。

由于指定参数文件名必须在控制器方法中file,但未在客户端代码中设置,因此发生此错误

解决它的两种方法:

一个。 删除@RequestParam("file")以避免指定参数名称

b. 将name属性添加到文件元素,如下所示:

<input type='file' name='file'>

相关内容

最新更新