繁体   English   中英

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

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

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

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

我不知道错误是什么。

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

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

春天需要

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

bean来处理文件上传。

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

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

EDIT1:

您可以将上传和内存大小赋予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>

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

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

在此输入图像描述

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM