1、springboot获取当前项目路径的地址

System.getProperty("user.dir")

输出目录: G:\outshine\wangsoso

2、获取classes目录绝对路径

// 方式1
String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();

// 方式2
String path = ResourceUtils.getURL("classpath:").getPath();

输出目录: /G:/outshine/wangsoso/target/classes/

3、如果上传目录为/static/upload/,则可以如下获取:

String path = ResourceUtils.getURL("classpath:").getPath();
System.out.println("path:"+path);
// path:/G:/Java/Maven/Project/server/target/classes/

File file = new File(path);
File upload = new File(file.getAbsolutePath(),"static/upload/");
// upload url: G:\Java\Maven\Project\server\target\classes\static\upload
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐