java项目发布到jar之后找不到文件路径
本地: String path = this.getClass().getClassLoader().getResource(xmlPath).getPath();
打成jar发布服务器不管用了
解决办法:
//相对路径
private final String xmlPath= "/jenkinspipeline/PipelineConfig.xml";
public StringBuilder propertyResolverXml(String xmlPath) {
InputStream resourceAsStream = XmlLocalUtils.class.getResourceAsStream(xmlPath);//读取内容
StringBuilder xmlStr = new StringBuilder();
BufferedReader read = null;
try {
read = new BufferedReader(new InputStreamReader(resourceAsStream));//转成bufferreader按行读取
String tempStr;
while ((tempStr = read.readLine()) != null) {
xmlStr.append(tempStr);
}
read.close();
} catch (Exception ex) {
log.error("解析文件异常:", ex);
throw new NativeSphereCicdException(NativeSphereCicdEnum.PROPERTYRESOLVERXML_EXCEPTION);
}
return xmlStr;
}