解决spring boot 执行jar包无法读取资源文件问题,copyInputStreamToFile到具体文件,再读取文件
String[] strPath = new String[]{"template_freemarker_fs.html","simhei.ttf"};
ApplicationHome homeSource = new ApplicationHome(getClass());
File jarFlod = homeSource.getSource();
System.out.println(jarFlod.getParentFile().toString());
String jarFilepath = jarFlod.getParentFile().toString();
for(int i=0;i<2;i++){
InputStream inputStreamFreemarker = Resources.getResourceAsStream(strPath[i]);
String filePath = jarFilepath+"/"+strPath[i];
System.out.println("filePath is:"+filePath );
//在根目录生成一个文件
File targetFile = new File(filePath);
if(!targetFile.exists()){
//将流转成File格式
copyInputStreamToFile(inputStreamFreemarker, targetFile);
inputStreamFreemarker.close();
}
}