【问题标题】:Spring boot running in tomcat, required request part 'file' is not present在 tomcat 中运行的 Spring Boot,所需的请求部分“文件”不存在
【发布时间】:2021-11-08 08:43:12
【问题描述】:

过去 2 天我一直在阅读堆栈溢出问题的解决方案。我已经尝试了其他人问题的所有答案,但对我没有任何帮助。当我通过 Eclipse 运行 Spring Boot 作为 Spring Boot 应用程序运行 Spring Boot 时,此方法有效,但是当我创建 War 并将其放置在 tomcat 上时,我收到“Required request part 'file' is not present”错误。

这是我用作示例的文章:https://www.baeldung.com/spring-file-upload

错误: org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver:logException(207): Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present]

Chrome 网络表单数据:

内容配置:表单数据;名称=“文件”;文件名="Flow_Results.xlsx" 内容类型:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

文件以传递给网络服务器的形式存在。

Pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- Tomcat -->
        <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-tomcat</artifactId>
           <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.4</version>
        </dependency>

Application.properties

spring.servlet.multipart.enabled=true
spring.servlet.multipart.location=${java.io.tmpdir}

Angular 函数发帖。

saveData(file: File, opertatorId: string): Observable<HttpEvent<{}>> {
    const formData: FormData = new FormData();
    formData.append('file', file, file.name);

    const newRequest = new HttpRequest('POST', 'cliareport/savedatafile/' + opertatorId, formData, {
        reportProgress: true,
        responseType: 'text',
    });

    return this.httpClient.request(newRequest);
}

Spring 启动控制器

@Bean
public CommonsMultipartResolver multipartResolver() {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    multipartResolver.setMaxUploadSize(-1);
    return multipartResolver;
}

@PostMapping(path="/cliareport/savedatafile/{operatorid}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<String> saveDataFile(@RequestParam("file") MultipartFile file,
    @PathVariable("operatorid") String operatorid) {
}

【问题讨论】:

    标签: java angular spring-boot file-upload tomcat9


    【解决方案1】:

    对于遇到此问题的任何人,这是 NIO SSL 连接器与 Ubuntu 20 版本附带的 tomcat 版本的问题。要解决此问题,请升级您的 tomcat 版本。

    https://bugs.launchpad.net/ubuntu/+source/tomcat9/+bug/1903851

    【讨论】:

      猜你喜欢
      • 2017-04-06
      • 2019-09-08
      • 2020-10-22
      • 2017-10-09
      • 2019-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多