最近发现java本地读取resource的资源是可以访问到的,但是打成jar包放到服务器中就会有访问不到资源文件的情况发生:
图片位置如下:
打包运行后报错:找不到文件目录
后来经过调整发现,这种资源文件在jar包中直接读取是取不到的,有种实现方式是通过文件流的方式拿到服务器再读取文件,这样就可以顺利读取
代码如下:
ClassPathResource resource = new ClassPathResource();
InputStream input = resource.getInputStream(“static/PDF/PDFClient.properties”);//资源文件路径
int index;
byte[] bytes = new byte[1024];
FileOutputStream downloadFile = new FileOutputStream(“/home/app/PDFClient.properties”);//输出文件目录
while ((index = input.read(bytes)) != -1) {
downloadFile.write(bytes, 0, index);
downloadFile.flush();
}
downloadFile.close();
input.close();
SecurityEngineDeal.setProfilePath("/home/app");//读取目录
最终读取文件完美解决!