所需的multipartfile参数“file”不存在[postman和springboot]

jogvjijk  于 2021-06-26  发布在  Java
关注(0)|答案(2)|浏览(547)

我正在传递一个csv文件作为参数,但它不是在后端代码接收。我研究了多种可用的解决方案,但似乎没有一种适合我。代码如下:

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

以下是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>

即使在@requestparam注解中使用“required=false”,也会发现多部分文件为空,即使inspect工具显示已将其作为请求发送。感谢您的帮助!
附言: Postman 剪:

头剪:

yqlxgs2m

yqlxgs2m1#

简单的回答,尝试使用 @RequestPart("file") 而不是 @RequestParam("file") . 请参阅此处的文档:https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/requestpart.html

zf2sa74q

zf2sa74q2#

结果,来自@berguigamohamedamine的评论让我意识到multipart要么不兼容,要么与springwebflux有一些问题。在我的例子中,我将multipart更改为filepart,现在一切正常。以下是最新的正文:

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

ps:@requestparam可以用来代替@requestpart,但是使用它是有意义的,因为我们试图将一个部分作为参数发送,所以我使用它:]

相关问题