简体   繁体   English

所需的 MultipartFile 参数“文件”不存在 [Postman 和 Springboot]

[英]Required MultipartFile parameter 'file' is not present [Postman and Springboot]

I'm passing a csv file as a parameter, but it is not being received at the backend code.我将 csv 文件作为参数传递,但后端代码未收到该文件。 I've looked into multiple available solutions, but none of them seemed to work for me.我研究了多种可用的解决方案,但似乎没有一个对我有用。 Here's the code:这是代码:

@PostMapping(value = "/import",  consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public Object uploadCSVFile(@RequestParam("file") MultipartFile csvFile, Model model) {
    String response = "";
    Object obj = null;
    if (csvFile.isEmpty()) {
        model.addAttribute("message", "Please select a CSV file to upload.");
        model.addAttribute("status", false);
    } else {
        try{
               //doo foo
            }
        catch (Exception e){
            System.out.println(e.getStackTrace());
        }
    }
    return obj;
}

Here are the dependencies in pom.xml if it's required for reference:如果需要参考,以下是 pom.xml 中的依赖项:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-r2dbc</artifactId>
    </dependency>
    <dependency>
        <groupId>io.r2dbc</groupId>
        <artifactId>r2dbc-h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <!-- Apache Commons FileUpload -->
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.1</version>
    </dependency>
    <!-- Apache Commons IO -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>
    <dependency>
        <groupId>com.opencsv</groupId>
        <artifactId>opencsv</artifactId>
        <version>5.0</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
        <version>2.2.5.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-core</artifactId>
        <version>9.0.39</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-csv</artifactId>
        <version>1.8</version>
    </dependency>
    <dependency>
        <groupId>org.jetbrains</groupId>
        <artifactId>annotations</artifactId>
        <version>RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
    </dependency>
</dependencies>

Even on using "required=false" in the @RequestParam annotation, turns out the Multipart file is null, even though the inspect tool displays having sent it as a request.即使在@RequestParam 注释中使用“required=false”,即使检查工具显示已将其作为请求发送,Multipart 文件也是 null。 Any help is appreciated!任何帮助表示赞赏!

PS: Postman Snip: PS:Postman 截图: 在此处输入图像描述

Headers snip:标题剪断: 在此处输入图像描述

Simple answer, try using @RequestPart("file") instead of @RequestParam("file") .简单的答案,尝试使用@RequestPart("file")而不是@RequestParam("file") See documentation here: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestPart.html请参阅此处的文档: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestPart.ZFC35FDC70D5269D23AEZC788

Turns out, the comment from @BERGUIGAMohamedAmine made me realize that MultiPart is either not compatible / or has some issues with Spring webflux.事实证明,@BERGUIGAMohamedAmine 的评论让我意识到 MultiPart 要么不兼容 / 要么与 Spring webflux 存在一些问题。 In my case, I changed MultiPart to FilePart, and things are now working fine.就我而言,我将 MultiPart 更改为 FilePart,现在一切正常。 Here's the updated body:这是更新后的正文:

@PostMapping(value = "/import",  consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Object uploadCSVFile(@RequestPart("file") FilePart file, Model model) {
        //do foo
}

PS: @RequestParam can be used instead of @RequestPart, but it made sense to use it since we are trying to send a Part as a parameter, so I used it:] PS:可以使用@RequestParam 代替@RequestPart,但是使用它是有意义的,因为我们试图将Part 作为参数发送,所以我使用了它:]

暂无
暂无

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

相关问题 Postman - 所需的 MultipartFile 参数“文件”不存在 - Postman - Required MultipartFile parameter 'file' is not present 在Spring 4.3.1中不存在必需的MultipartFile参数&#39;file&#39; - Required MultipartFile parameter 'file' is not present in spring 4.3.1 错误消息=必需的MultipartFile参数&#39;file&#39;不存在 - Error message = Required MultipartFile parameter 'file' is not present spring mvc 中不存在所需的 MultipartFile 参数“文件” - Required MultipartFile parameter 'file' is not present in spring mvc Spring + ExtJS 文件上传所需的 MultipartFile 参数“文件”不存在 - Spring + ExtJS File Upload Required MultipartFile parameter 'file' is not present 上传文件时不存在必需的MultipartFile参数“文件” - Required MultipartFile parameter 'file' is not present when uploading file 尝试上传文件时,所需的MultipartFile参数&#39;file&#39;不存在错误 - Required MultipartFile parameter 'file' is not present error when trying upload a file 处理程序执行导致异常:所需的MultipartFile参数&#39;file&#39;不存在 - Handler execution resulted in exception: Required MultipartFile parameter 'file' is not present 获取HTTP状态400 - 必需的MultipartFile参数&#39;file&#39;在spring中不存在 - Geting HTTP Status 400 - Required MultipartFile parameter 'file' is not present in spring 必需的MultipartFile参数&#39;file&#39;不存在Java Spring MVC - Required MultipartFile parameter 'file' is not present java spring mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM