Springboot2.x——Jar运行静态资源报错问题

本文探讨了在SpringBoot项目中,IDE内直接运行与打包成Jar后运行时,读取资源文件的不同方法及遇到的问题。详细介绍了如何根据不同环境调整读取路径,确保程序在不同部署条件下都能正确加载资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Springboot打成Jar包运行,和在Idea内启动读取资源文件的方法是不一样的,导致部署项目的时候出了点问题

一、编译器内直接springboot项目运行:

properties去读数据:

public class GetProUtil {
   private String file;
   private Properties prop;
   
   public GetProUtil(String file) {
      this.file = file;
      this.prop = readProperties();
   }
   
   private Properties readProperties(){
      Properties properties = new Properties();
      InputStream inputStream;
      try {
         inputStream = new FileInputStream(file);
         BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
         properties.load(bf);
         inputStream.close(); // 关闭流
      } catch (Exception e) {
         throw new RuntimeException(e.getMessage());
      }
      return properties;
   }
}

方法调用可行

public static GetProUtil iot = new GetProUtil("src/main/resources/iot.properties");

二、springboot打成jar包去运行:

报错如下:无法找到该路径文件

Caused by: java.lang.RuntimeException: src/main/resources/iot.properties (No such file or directory)
at com.wx.utils.GetProUtil.readProperties(GetProUtil.java:56)
at com.wx.utils.GetProUtil.<init>(GetProUtil.java:37)
at com.wx.mqtt.WxVariable.<clinit>(WxVariable.java:8)
... 13 more

将读取路径修改成流:

private Properties readProperties(){
   Properties properties = new Properties();
   InputStream inputStream;
   try {
      //inputStream = new FileInputStream(file);
      
      ClassPathResource resource = new ClassPathResource(file);
      inputStream = resource.getInputStream();
      
      File resourceFile = resource.getFile();
      //BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(resourceFile)));
      //String msg = reader.readLine();
      
      BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
      properties.load(bf);
      inputStream.close(); // 关闭流
   } catch (Exception e) {
      throw new RuntimeException(e.getMessage());
   }
   return properties;
}

调用方法修改成:

public static GetProUtil iot = new GetProUtil("iot.properties");

springboot jar 运行报错:路径:/Users/wuxi/RunJar/springboot-mqtt-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/iot.properties

Caused by: java.lang.RuntimeException: class path resource [iot.properties] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/Users/wuxi/RunJar/springboot-mqtt-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/iot.properties
at com.wx.utils.GetProUtil.readProperties(GetProUtil.java:59)
at com.wx.utils.GetProUtil.<init>(GetProUtil.java:28)
at com.wx.mqtt.WxVariable.<clinit>(WxVariable.java:9)
... 13 more

发现该行代码报错:

File sourceFile = resource.getFile();

macos linux系统下面 需要使用

ClassPathResource resource = new ClassPathResource(file);
inputStream = resource.getInputStream();

注释掉再次运行OK

方便使用:直接定义两种获取方式

private Properties readProperties(){
   Properties properties = new Properties();
   InputStream inputStream;
   try {
      if (file.contains("src")) {
         inputStream = new FileInputStream(file);
      }else {
         ClassPathResource resource = new ClassPathResource(file);
         inputStream = resource.getInputStream();
      }
      BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
      properties.load(bf);
      inputStream.close(); // 关闭流
   } catch (Exception e) {
      throw new RuntimeException(e.getMessage());
   }
   return properties;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值