注意事项,必须先检查!
检查是否将文件放入新建的文件夹中,务必新建,否则无法读取!
本文示例获取photo文件夹下的文件。
以下2种方式,亲测可用
- 使用ClassPathResource类获取
ClassPathResource classPathResource = new ClassPathResource("/photo/tencentPicture01.jpg");
InputStream inputStream = classPathResource.getInputStream();
- 使用ResourceUtils类获取
File file = ResourceUtils.getFile("classpath:photo/tencentPicture01.jpg");
以上两个类都来自于springframework,无需再引用其它工具包。