Last active
June 18, 2019 07:19
-
-
Save vector4wang/4caceb0f714352f92f8b29de6ee5373f to your computer and use it in GitHub Desktop.
[springboot读取资源文件] 万金油? #Java #Springboot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 方法1:获取文件或流 | |
this.getClass().getResource("/")+fileName; | |
this.getClass().getResourceAsStream(failName); | |
// 方法2:获取文件 | |
File file = org.springframework.util.ResourceUtils.getFile("classpath:test.txt"); | |
// 方法3:获取文件或流 | |
ClassPathResource classPathResource = new ClassPathResource("test.txt"); | |
classPathResource .getFile(); | |
classPathResource .getInputStream(); | |
// >>>>>>>>>>>>>>>> 下面方法可以读取jar包下文件 | |
假设resources目录下有一个test.txt文件,首先获得当前的类加载器,通过类加载器读取文件。 | |
// 方法1 | |
InputStream io = Thread.currentThread().getContextClassLoader().getResourceAsStream("test.txt"); | |
// 方法2 | |
InputStream io = getClass().getClassLoader().getResourceAsStream("test.txt"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment