首页 > 解决方案 > Spring 4.3.1 中不存在必需的 MultipartFile 参数“文件”

问题描述

我正在使用 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 文件中添加了 multipartResolver。

 <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

我怎样才能解决这个问题?请帮我。感谢您的观看。

标签: javaspringspring-mvcng-file-upload

解决方案


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

解决它的两种方法:

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

湾。将name属性添加到您的文件元素,如下所示:

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

推荐阅读