java 获取jar包内外的配置文件

程序运行,

有时候需要获取jar包内内容, 例如版本更新信息,新增文本提示等

有时候需要获取jar包外内容, 例如每个系统运行的环境, 系统名称等

 

当前用的是spring clud框架

获取到jar包内配置文件

String fileName = "文件名.xml";

InputStream is = this.getClass().getResourceAsStream("/"+fileName); //通过java getclass 获取到jar包内文件, 获取到输入流

 

 private static Map<String, ObjectNode> verMap = new HashMap<>();

if (document == null)
return;

List<?> tables = document.selectNodes("UrlList"); //通过document 的方法, 解析xml

if (tables != null && tables.size() > 0) {
for (Object obj : tables) {
Element urlList = (Element) obj;

for (Object urlObj : urlList.selectNodes("url")) {
Element urlElement = (Element) urlObj;

String verid = urlElement.attributeValue("verid");
String vernum = urlElement.attributeValue("vernum");
String vertime = urlElement.attributeValue("vertime");
String vertext = urlElement.getText();

if (!verMap.containsKey(verid)){
ObjectNode objectNode = Convert.newObject();

objectNode.put("verid", verid);
objectNode.put("vernum", vernum);
objectNode.put("vertime", vertime);
objectNode.put("vertext", vertext);

verMap.put(verid, objectNode);
}
}
}
}

 

<?xml version="1.0" encoding="UTF-8"?>
<UrlList>

<url verid='2' vernum='1.1' vertime='2019/12/12'>
2、
</url>

<url verid='1' vernum='1.0' vertime='2019/12/12'>
1、
</url>

</UrlList>

通过ConfigUtil.getApplicationPath()获取到项目运行绝对地市

File.separator 斜线, 文件夹符号

String configPath = "file:"+ConfigUtil.getApplicationPath()+File.separator+"conf"+File.separator+"applicationContext.xml";

 

posted @ 2021-02-24 15:46  微臣在此  阅读(888)  评论(0)    收藏  举报