1、项目中有一些文件放在“src\main\resources\templates\”的下面,代码需要得到里面某个文件的路径。本地开发的时候没有问题。
2、线上报错,找不到文件。这是因为运行环境的问题,开发在w10,线上是linux。
3.根据环境获取路径
// 操作系统名称
String osName = System.getProperties().getProperty("os.name").toLowerCase();
String imageUrl="";
if(osName.contains("window")){
// windows环境
imageUrl= System.getProperty("user.dir") + "/src/main/resources/templates/aaa.jpg";
}else{
//linux环境
imageUrl=getClass().getClassLoader().getResource("templates/aaa.jpg").getPath();
}
4、这样就拿到了不同运行环境下,项目中文件的不同地址。