温馨提示×

springboot怎么获取resource文件路径

小亿
393
2024-02-04 09:19:14
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在Spring Boot中,可以使用ResourceLoader来获取resource文件的路径。

@Autowired
private ResourceLoader resourceLoader;

public void getResourcePath() throws IOException {
    Resource resource = resourceLoader.getResource("classpath:myfile.txt");
    String filePath = resource.getFile().getAbsolutePath();
    System.out.println("Resource file path: " + filePath);
}

在上面的例子中,resourceLoader.getResource()方法可以接受一个资源路径作为参数,其中classpath:前缀表示资源文件位于classpath路径下。getFile()方法用于获取资源文件的绝对路径。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:springboot怎么获取resource路径

0