Springboot 生产环境下读取Resource下的文件

第一种:
ClassPathResource classPathResource = new ClassPathResource("excleTemplate/test.xlsx");
InputStream inputStream =classPathResource.getInputStream();


第二种:
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("excleTemplate/test.xlsx");

第三种:
InputStream inputStream = this.getClass().getResourceAsStream("/excleTemplate/test.xlsx");


第四种:
File file = ResourceUtils.getFile("classpath:excleTemplate/test.xlsx");
InputStream inputStream = new FileInputStream(file);
//生产报错:FileNotFoundException: class path resource [template/fileContent.txt] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/external-service.jar!/BOOT-INF/classes!/template/fileContent.txt

前三种方法在开发环境(IDE中)和生产环境(linux部署成jar包)都可以读取到,第四种只有开发环境 时可以读取到,生产环境读取失败。

猜你喜欢

转载自blog.csdn.net/weixin_46267445/article/details/122328268