javaweb中获取文件路径的方法

24 篇文章 0 订阅
/**
* 获得文件路径的方法
* @param indexofValue 要截取到的路径 1、"WEB-INF/classes":截取WebRoot 2、"classes":WEB-INF
* @param path 传入想要获得的WebRoot下的文件夹已经文件名
* @return
*/
public String getPath(String indexofValue, String path){
// 获得路径,HuaWeiYgxjDao为类名
String folderPath = HuaWeiYgxjDao.class.getProtectionDomain().getCodeSource().getLocation().getPath();
if (folderPath.indexOf("WEB-INF") > 0) {
path = folderPath.substring(1, folderPath .indexOf(indexofValue)) + path;
}
path = path.replaceAll("%20", " "); //如果路径中有空格获取出来会是%20,需要替换
return path;
}

获取方法
String modelPath = publicMethod.getPath("WEB-INF/classes","FileModels_Huawei");//这样获得项目下FileModels_Huawei文件夹,不是WebRoot下
String folderPath = publicMethod.getPath("classes",configName);//获取WebRoot下的文件夹
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值