springboot 获取项目的绝对路径

新建SpringBoot 项目,项目名为fileupload

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

System.getProperty("user.dir")
//输出目录:F:\fileupload\fileupload

二、获取classes目录绝对路径

//获取classes目录绝对路径  方式一
String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
//输出目录:/F:/fileupload/fileupload/target/classes/
//获取classes目录绝对路径  方式二
String path = ResourceUtils.getURL("classpath:").getPath();
//输出目录:/F:/fileupload/fileupload/target/classes/

二、获取静态资源路径

如word模板放倒项目的如下位置:
在这里插入图片描述

//word模板地址获取方式一:缺点---打jar包获取不到该路径
String basePath=ClassUtils.getDefaultClassLoader().getResource("").getPath()+"static/template/";
String resource =basePath+"order1.docx";//word模板地址
	        
//word模板地址获取方式二:优点---相比上一种方式,这种方法不会在linux或者jar上失效
ClassPathResource classPathResource = new ClassPathResource("static/template/order1.docx");
String resource = classPathResource.getURL().getPath();
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值