java 获取 HTTP 状态 400 - Spring 中不存在必需的 MultipartFile 参数“文件”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32990908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:03:01  来源:igfitidea点击:

Geting HTTP Status 400 - Required MultipartFile parameter 'file' is not present in spring

javaspringfile-upload

提问by Labeo

I am trying to upload a file using spring. Below is my code how I am working on it but if I try to use it I am getting this response:

我正在尝试使用spring. 下面是我的代码,我是如何处理它的,但如果我尝试使用它,我会得到这个response

HTTP Status 400 - Required MultipartFile parameter 'file' is not present

HTTP 状态 400 - 所需的 MultipartFile 参数“文件”不存在

I dont get what the error is.

我不明白错误是什么。

I am using advanced rest client for testing and I am uploading file as an attachment.

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

My Javacode:

我的Java代码:

@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.";
        }
    }

采纳答案by Patrick

Spring needs the

春天需要

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

bean to handle file-uploads.

bean 来处理文件上传。

You should register this bean in your application contextfile.

你应该在你的application context文件中注册这个 bean 。

The Content-Type should also be valid. In your case enctype="multipart/form-data"

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

EDIT1:

编辑1:

You can give the upload and memory size to the bean properties:

您可以为 bean 属性提供上传和内存大小:

  <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- max upload size in bytes -->
        <property name="maxUploadSize" value="20971520" /> <!-- 20MB -->

        <!-- max size of file in memory (in bytes) -->
        <property name="maxInMemorySize" value="1048576" /> <!-- 1MB -->

    </bean>

回答by Sanjay Singh Rawat

when you select the file in advance rest client, on the right sidethere is a input box, write in that input box name of the parameter, in your case name of parameter is file

当您预先选择文件rest client时,右侧有一个输入框,在该输入框中输入参数名称,在您的情况下,参数名称为文件

Parameter name defined here in controller @RequestParam("file")

此处定义的参数名称在控制器@RequestParam("file")

enter image description here

在此处输入图片说明

回答by ed u

it works for me after i wrote in that input box name of the parameter "file" when i already have well configured the bean id in the Krajee bootstrap inputfile example ("https://github.com/kartik-v/bootstrap-fileinput").

当我已经在 Krajee 引导程序输入文件示例(“ https://github.com/kartik-v/bootstrap-fileinput”)。