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 extends BaseController {

@RequestMapping(value = "path")
    public String path(Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        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) throws Exception {
        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) throws Exception {
        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) throws Exception {
        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) throws Exception {
        //加载配置文件方式一:
        FileInputStream fis = new FileInputStream(classLoader.getResource("config/j2cache.properties").getPath());
        Properties prop=new Properties();
        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=new Properties();
        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";
    }

猜你喜欢

转载自www.cnblogs.com/pluto-yang/p/12574697.html