springboot读取resources下的文件
第一种方法:
resources: static-locations: file:c:/files/,classpath:/document/,classpath:/static/ mvc: static-path-pattern: /**
第二种方法:
pom.xml配置文件路径
<resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> <include>**/*.html</include> <include>**/*.xlsx</include> <include>**/*.docx</include> <include>/static/</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> </includes> <filtering>false</filtering> </resource> </resources>
读取文件为InputStream的三种方法
一、
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("文件路径");
二、
InputStream inputStream = this.getClass().getResourceAsStream("文件路径");
ResourceUtils和ClassPathResource方式开发环境下可以读取到、生产环境下读取不到文件,因为项目在服务器启动的时候被打成了jar包,无法直接读取文件。其他三种都属于类加载器读取文件流。
ResourceUtils和ClassPathResource用法:
File file = ResourceUtils.getFile("classpath:excleTemplate/test.xlsx");
InputStream inputStream = new FileInputStream(file);
new ClassPathResource("/static/temp12.docx").getFile()