SpringBoot从Tomcat变为Undertow踩坑记录

maven配置

        <!-- SpringBoot Web容器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <!-- 排除tomcat -->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- undertow 容器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>

pom.xml只需如上设置就能去除SpringBoot内置的Tomcat,改为Undertow容器

踩坑记录

1、file.transferTo()方法失效

报错:

java.nio.file.FileAlreadyExistsException:C:\data\crm\uploadPath\upload\2023\02\02\c9bcccaa-ec94-423b-8609-499033049cc0.pdf

解决方法:

// file.transferTo(desc);
FileUtils.copyInputStreamToFile(file.getInputStream(), desc);

2、上传文件会出现临时文件目录不存在的错误

报错:

org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.lang.RuntimeException: java.nio.file.NoSuchFileException: C:\Users\ADMINI~1\AppData\Local\Temp\undertow.8107.8986308911172817130\undertow662609047623901049upload

原因:是由于临时 /tmp 目录下的文件,在长时间(10天)没有使用的情况下,系统执行了 tmp 目录清理服务,导致 /tmp/undertow.8107文件被清理,然而在上传的时候,undertow 服务器需要创建/tmp/undertow.8107/undertow…upload 临时文件,但是调用 Files.createFile(…) 的时候就会发现找不到父目录,才导致了以上的错误。

解决方法:

-Djava.io.tmpdir=/your/temp/path/

启动jar的命令中添加以上启动参数即可
参考:http://t.csdnimg.cn/t6Rjj

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值