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;
}