// 加载配置文件 ClassPathResource classPathResource = new ClassPathResource("template/books.xml"); InputStream inputStream =classPathResource.getInputStream();
// 加载配置文件 InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("template/books.xml");
InputStream inputStream = this.getClass().getResourceAsStream("template/books.xml");
File file = ResourceUtils.getFile("classpath:template/books.xml"); InputStream inputStream = new FileInputStream(file);
File directory = new File("src/main/resources"); String courseFile = directory.getCanonicalPath();//这种方式也能获取到一样的结果
- spring获得文件路径
// 获得路径在target下 String path = BookController.class.getClass().getResource("/com/scba/modules/book/books.xml").getPath(); // 获得路径在源码src下 String path = ResourceUtils.getFile("src/main/java/com/scba/modules/book/books.xml").getAbsolutePath();