1、
String contextPath = request.getContextPath();
String realPath = request.getSession().getServletContext().getRealPath("/");
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+contextPath+"/";
2、
contextPath = "/项目名称"; //获取的是项目的相对路径
realPath = F:\tomcat_home\webapps\项目名称\ //获取的是项目的绝对路径
basePath = http://localhost:8080/项目名称/ //获取的是服务的访问地址
String contextPath = request.getContextPath();
String realPath = request.getSession().getServletContext().getRealPath("/");
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+contextPath+"/";
2、
contextPath = "/项目名称"; //获取的是项目的相对路径
realPath = F:\tomcat_home\webapps\项目名称\ //获取的是项目的绝对路径
basePath = http://localhost:8080/项目名称/ //获取的是服务的访问地址
本文介绍了在Web开发中获取应用程序不同路径的方法,包括上下文路径(contextPath)、真实路径(realPath)和服务的基础路径(basePath)。这些路径对于配置资源位置、进行文件操作等非常重要。
222

被折叠的 条评论
为什么被折叠?



