方法一:在linux跟windows下,将springboot打包成war,可以获取,但是springboot打包成jar包就找不到该文件了
String path = Thread.currentThread().getContextClassLoader().getResource("app.xml").getPath();
System.out.println("------path---" + path);
File file = new File(path);
if (file.exists()) {
System.out.println("----文件存在---");
FileInputStream inputStream = new FileInputStream(file);
int len = 0;
byte[] bytes = new byte[inputStream.available()];
inputStream.read(bytes);
String strXml = new String(bytes, "utf-8");
} else {
System.out.println("---no xml---------");
}
方法二:在linux跟windows下,springboot打包成jar包跟war都是可以的
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("app.xml");
if(inputStream!=null){
System.out.println("----文件存在---");
// FileInputStream inputStream = new FileInputStream(file);
int len = 0;
byte[] bytes = new byte[inputStream.available()];
inputStream.read(bytes);
String strXml = new String(bytes, "utf-8");
else {
System.out.println("---no xml---------");
}