springboot上传头像放在resources后,读取不到,待项目重新启动后能获取(解决)

1、由于上传的图片在相对路径,前端上传图片后,不会加载到资源文件中。所以第二次访问会找不到图片,但是图片是缺失存在的。
2、添加映射访问,在这个里面加上你存放图片的绝对路径即可。

.addResourceLocations("file:"+new File("src/main/resources/"+avatar).getAbsolutePath()+"\\")
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
在Spring Boot项目中,可以通过在`application.properties`或`application.yml`配置文件中定义配置属性,然后在项目启动时使用`@Value`注解注入属性值来读取配置文件数据。 以`application.properties`为例,首先在`src/main/resources`目录下创建该文件,并定义需要读取的配置属性,例如: ```properties # 数据库连接配置 spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=password ``` 然后在需要使用该配置属性的类中使用`@Value`注解注入属性值,例如: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class DatabaseConfig { @Value("${spring.datasource.url}") private String url; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; // getters and setters } ``` 在上面的代码中,通过在`@Value`注解中使用`${}`来引用配置属性,然后将属性值注入到对应的变量中。 需要注意的是,使用`@Value`注解注入属性值的类必须是Spring Bean,因此需要在类上添加`@Component`注解或其它符合条件的注解,以便Spring能够扫描并创建该类的实例。同时,需要在Spring Boot应用程序的入口类上添加`@EnableConfigurationProperties`注解,以启用注入属性值的功能。 另外,如果需要读取`application.yml`配置文件中的属性值,可以使用类似的方式,并在`@Value`注解中使用`:`来引用属性,例如: ```yaml # 数据库连接配置 spring: datasource: url: jdbc:mysql://localhost:3306/mydb username: root password: password ``` ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class DatabaseConfig { @Value("${spring.datasource.url}") private String url; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; // getters and setters } ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

syf_wfl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值