java web获取配置文件路径_JavaWeb项目中获取路径、classLoader.getResourceAsStream加载配置文件、URL/URI...

请求路径:http://localhost:8081/coll-web/a/test/path

一、网络路径

1、URI、URL

URL:包括ip端口的请求全路径

URI:不包括ip和端口

@Controller

@RequestMapping(value= "${adminPath}/test")public class TestController extendsBaseController {

@RequestMapping(value= "path")public String path(Model model, HttpServletRequest request, HttpServletResponse response) throwsException {

System.out.println("uri-----"+request.getRequestURI()); ///coll-web/a/test/path

System.out.println("url-----"+request.getRequestURL()); // http://localhost:8081/coll-web/a/test/path

}

}

二、文件路径

1、ContextPath、ServletPath

ContextPath:项目根路径

ServletPath:servlet路径

@RequestMapping(value = "path")public String path(Model model, HttpServletRequest request, HttpServletResponse response) throwsException {

System.out.println("1-----"+request.getContextPath()); ///coll-web

System.out.println("2-----"+request.getServletPath()); ///a/test/path

}

2、ServletContext

@RequestMapping(value = "path")public String path(Model model, HttpServletRequest request, HttpServletResponse response) throwsException {

ServletContext servletContext=request.getServletContext();

System.out.println("3---"+servletContext.getContextPath()); ///coll-web

System.out.println("4---"+servletContext.getRealPath("")); //null

System.out.println("5---"+servletContext.getRealPath("/")); //null

System.out.println("6---"+servletContext.getRealPath("static/favicon.ico"));

//C:\Users\pluto\AppData\Local\Temp\tomcat-docbase.6106731299044632940.8081\static\favicon.ico

System.out.println("---"+servletContext.getRealPath("/static/favicon.ico"));

//C:\Users\pluto\AppData\Local\Temp\tomcat-docbase.6106731299044632940.8081\static\favicon.ico

System.out.println("7---"+servletContext.getRealPath("config/beetl.properties"));

//C:\Users\pluto\AppData\Local\Temp\tomcat-docbase.6106731299044632940.8081\config\beetl.properties

System.out.println("---"+servletContext.getRealPath("/config/beetl.properties"));

//C:\Users\pluto\AppData\Local\Temp\tomcat-docbase.6106731299044632940.8081\config\beetl.properties

}

3、ClassLoader

classLoader.getResource("").getPath():获取当前环境下的classes文件路径

@RequestMapping(value = "path")public String path(Model model, HttpServletRequest request, HttpServletResponse response) throwsException {

ClassLoader classLoader= this.getClass().getClassLoader();

System.out.println("8---"+classLoader.getResource("").getPath());

///C:/Develop/Idea/workspace2/coll-web/jeesite-web/src/main/webapp/WEB-INF/classes/

System.out.println("9---"+classLoader.getResource("static/favicon.ico").getPath());

///C:/Develop/Idea/workspace2/coll-web/jeesite-web/src/main/webapp/WEB-INF/classes/static/favicon.ico

System.out.println("10---"+classLoader.getResource("config/beetl.properties").getPath());

///C:/Develop/Idea/workspace2/coll-web/jeesite-web/src/main/webapp/WEB-INF/classes/config/beetl.properties//System.out.println("11---"+classLoader.getResource("/").getPath());//报错//System.out.println("12---"+classLoader.getResource("/static/favicon.ico").getPath());//报错//System.out.println("13---"+classLoader.getResource("/config/beetl.properties").getPath());//报错

}

三、通过classLoader加载配置文件

@RequestMapping(value = "path")public String path(Model model, HttpServletRequest request, HttpServletResponse response) throwsException {//加载配置文件方式一:

FileInputStream fis = new FileInputStream(classLoader.getResource("config/j2cache.properties").getPath());

Properties prop=newProperties();

prop.load(fis);

System.out.println(prop);//properties里的k v键值对//根据key获取value

System.out.println("14---"+prop.get("redis.namespace")); //jeesite

System.out.println("15---"+prop.getProperty("redis.namespace")); //jeesite//加载配置文件方式二:

InputStream is = classLoader.getResourceAsStream("config/j2cache.properties");

Properties prop2=newProperties();

prop2.load(is);

System.out.println(prop2);

System.out.println("16---"+prop2.get("redis.namespace")); //jeesite

System.out.println("17---"+prop2.getProperty("redis.namespace")); //jeesite

return "modules/sysLogin";

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值