gpt4 book ai didi

java - 上传多个文件在 spring boot Controller 中不起作用

转载 作者:行者123 更新时间:2023-12-05 06:14:36 25 4
gpt4 key购买 nike

我可以像这样将一个文件上传到 spring boot Controller :

@RequestMapping(value = "/projects",
produces = { "application/json" },
consumes = { "multipart/form-data" },
method = RequestMethod.POST)
public ResponseEntity<Void> upload(@RequestParam(value="file", required=false) MultipartFile files)) {
//
}

但我无法将多个文件上传到类似的 spring boot Controller :

@RequestMapping(value = "/projects",
produces = { "application/json" },
consumes = { "multipart/form-data" },
method = RequestMethod.POST)
public ResponseEntity<Void> upload(@RequestParam(value="files", required=false) MultipartFile[] files)) {
//
}
@RequestMapping(value = "/projects",
produces = { "application/json" },
consumes = { "multipart/form-data" },
method = RequestMethod.POST)
public ResponseEntity<Void> upload(@RequestParam(value="files", required=false) List<MultipartFile> files)) {
//
}

*当我尝试上传文件时,上面的两个 Controller 有空列表或数组

我的 spring boot 是否需要一些额外的配置来上传多个文件?

最佳答案

这是我的测试项目的配置:

  1. pom.xml

     <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>multipart-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>multipart-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
    <java.version>11</java.version>
    </properties>

    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
    <exclusion>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    </exclusion>
    </exclusions>
    </dependency>
    </dependencies>

    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
    </build>

    <repositories>
    <repository>
    <id>spring-milestones</id>
    <name>Spring Milestones</name>
    <url>https://repo.spring.io/milestone</url>
    </repository>
    <repository>
    <id>spring-snapshots</id>
    <name>Spring Snapshots</name>
    <url>https://repo.spring.io/snapshot</url>
    <snapshots>
    <enabled>true</enabled>
    </snapshots>
    </repository>
    </repositories>
    <pluginRepositories>
    <pluginRepository>
    <id>spring-milestones</id>
    <name>Spring Milestones</name>
    <url>https://repo.spring.io/milestone</url>
    </pluginRepository>
    <pluginRepository>
    <id>spring-snapshots</id>
    <name>Spring Snapshots</name>
    <url>https://repo.spring.io/snapshot</url>
    <snapshots>
    <enabled>true</enabled>
    </snapshots>
    </pluginRepository>
    </pluginRepositories>

    </project>
  2. MultipartDemoApplication.java

     package com.example.multipartdemo;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;

    @SpringBootApplication
    public class MultipartDemoApplication {

    public static void main(String[] args) {
    SpringApplication.run(MultipartDemoApplication.class, args);
    }
    }
  3. FileUploadController.java

     package com.example.multipartdemo;

    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.multipart.MultipartFile;

    import java.util.Arrays;
    import java.util.stream.Collectors;

    @Controller
    public class FileUploadController {
    @RequestMapping(value = "/projects",
    produces = {"application/json"},
    consumes = {"multipart/form-data"},
    method = RequestMethod.POST)
    public ResponseEntity<String> uploadFiles(@RequestParam(value = "files", required = false) MultipartFile[] files) {
    try {
    String fileNames = Arrays.stream(files)
    .map(MultipartFile::getOriginalFilename)
    .collect(Collectors.joining(" : "));
    return ResponseEntity.status(HttpStatus.OK).body(fileNames);
    } catch (Exception e) {
    return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body("Unable to download files");
    }
    }

    @RequestMapping(value = "/projects",
    produces = {"text/plain"},
    method = RequestMethod.GET)
    public ResponseEntity<String> uploadFiles() {
    return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body("OK");
    }
    }
  4. application.properties

     spring.servlet.multipart.max-file-size=100KB
    spring.servlet.multipart.max-request-size=500KB

启动应用程序后,它按预期工作:

Demo_multipart_form_data_results

只有在 body 不存在的情况下我才会得到下一个响应,但这是另一个调试故事。

Demo_multipart_form_file_exception

那么,你能检查一下你的配置吗?和我的有什么区别?

关于java - 上传多个文件在 spring boot Controller 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62827385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com