【问题标题】:Required MultipartFile parameter 'file' is not present in spring 4.3.1Spring 4.3.1 中不存在必需的 MultipartFile 参数“文件”
【发布时间】:2018-06-23 11:03:36
【问题描述】:

我使用的是 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

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

【问题讨论】:

    标签: java spring spring-mvc ng-file-upload


    【解决方案1】:

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

    两种方法解决:

    一个。去掉@RequestParam("file"),避免指定参数名

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

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

    【讨论】:

    • 那么我应该删除 @RequestParam("file") 还是将 name 属性添加到您的文件元素?
    • @alexKruts 取决于你自己,你可以尝试任何一个
    • @alexKruts 所以试试方法a
    • 则文件参数为空。
    • @alexKruts 现在错误消失了,您需要检查您的客户端代码,它在 php 服务器中运行良好并不意味着它在 java 服务器中运行良好
    猜你喜欢
    • 2015-01-21
    • 1970-01-01
    • 1970-01-01
    • 2014-09-10
    • 2014-05-08
    • 1970-01-01
    • 2017-02-12
    • 1970-01-01
    • 2019-03-14
    相关资源
    最近更新 更多