【SpringBoot】解决SpringBoot 打包启动项目后,静态资源文件不可用

问题描述

今天在启动项目时,发现网页的图标都加载不出来,打开控制台,发现告警信息是文件解码错误,错误信息:

  1. Failed to decode downloaded font: http://localhost:8080/plugins/fontawesome-free/webfonts/fa-solid-900.woff2
  2. OTS parsing error: Failed to convert WOFF 2.0 font to SFNT
  3. OTS parsing error: incorrect file size in WOFF header
  4. OTS parsing error: incorrect entrySelector for table directory

如下图:
在这里插入图片描述
在这里插入图片描述

原因分析

maven打包时:会编译项目文件,如果存在不需要编译的文件需要在配置里面排除。因为有些资源文件被编译后会与原文件不同,导致文件不可用。

问题解决

添加 maven 资源文件管理插件 maven-resources-plugin,指定不过滤的文件扩展名。

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        
        <!-- 不过滤字体文件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <configuration>
                <nonFilteredFileExtensions>
                    <nonFilteredFileExtension>ttf</nonFilteredFileExtension>
                    <nonFilteredFileExtension>woff</nonFilteredFileExtension>
                    <nonFilteredFileExtension>woff2</nonFilteredFileExtension>
                </nonFilteredFileExtensions>
            </configuration>
        </plugin>
    </plugins>
</build>

在这里插入图片描述
图标正常加载出来了,控制台也没有告警错误信息。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值