在SpringBoot中,可以使用ResourceLoader来获取resource文件的路径。 @Autowired privateResourceLoaderresourceLoader; public
在SpringBoot中,可以使用ResourceLoader
来获取resource文件的路径。
@Autowired
privateResourceLoaderresourceLoader;
publicvoidgetResourcePath()throwsIOException{
Resourceresource=resourceLoader.getResource("classpath:myfile.txt");
StringfilePath=resource.getFile().getAbsolutePath();
System.out.println("Resourcefilepath:"+filePath);
}
在上面的例子中,resourceLoader.getResource()
方法可以接受一个资源路径作为参数,其中classpath:
前缀表示资源文件位于classpath路径下。getFile()
方法用于获取资源文件的绝对路径。
版权声明
本文仅代表作者观点,不代表博信信息网立场。