zoukankan      html  css  js  c++  java
  • 关于获取路径path

    String webPath = request.getServletPath();
    log.info(webPath);
    输出:
    /zjdlbb/zjdlbb/zjdlbb/test.ht
    
    
    log.info(request.getServletContext().getRealPath("/"));
    输出:
    E:Program Files (x86)workspaceomsweb
    
    
    
    String webPath = request.getContextPath();
    log.info(webPath); 输出:
    /oms (项目名) // 使用相对目录的方式读取文件,相对目录即是项目的目录 File file = new File("src" + File.separator + "dom" + File.separator + "xml" + File.separator + "class.xml"); File file2 = new File(DomDemo.class.getClassLoader() .getResource("").getPath().substring(1) + "dom" + File.separator + "xml" + File.separator + "class.xml"); //当前路径 System.out.println(Demo01.class.getResource("."));// file:/D:/Program/workspace/freemarker/WebRoot/WEB-INF/classes/demo01/ System.out.println(Demo01.class.getResource(""));// file:/D:/Program/workspace/freemarker/WebRoot/WEB-INF/classes/demo01/ System.out.println(Demo01.class.getResource(".").getPath());// /D:/Program/workspace/freemarker/WebRoot/WEB-INF/classes/demo01/ System.out.println(Demo01.class.getResource("").getPath());// /D:/Program/workspace/freemarker/WebRoot/WEB-INF/classes/demo01/ //根目录 System.out.println(Demo01.class.getResource("/"));// file:/D:/Program/workspace/freemarker/WebRoot/WEB-INF/classes/ System.out.println(Demo01.class.getResource("/").getPath());// /D:/Program/workspace/freemarker/WebRoot/WEB-INF/classes/ //项目路径 System.out.println(System.getProperty("user.dir"));// D:Programworkspacefreemarker System.out.println(Demo01.class.getClassLoader().getResource("."));// file:/D:/Program/workspace/freemarker/WebRoot/WEB-INF/classes/ System.out.println(Demo01.class.getClassLoader().getResource(""));// file:/D:/Program/workspace/freemarker/WebRoot/WEB-INF/classes/ System.out.println(Demo01.class.getClassLoader().getResource(".").getPath());// /D:/Program/workspace/freemarker/WebRoot/WEB-INF/classes/ System.out.println(Demo01.class.getClassLoader().getResource("").getPath());// /D:/Program/workspace/freemarker/WebRoot/WEB-INF/classes/ 注意: 在java中获取文件路径的时候,有时候会获取到空格,但是在中文编码环境下,空格会变成“%20”从而使得路径错误. 解决办法: String path = Parameter.class.getResource("").getPath();//得到路径 path = URLDecoder.decode(path,"utf-8");//关键啊 !
  • 相关阅读:
    JavaScript深拷贝和浅拷贝
    JavaScript正则表达式(一)-常用方法
    面向对象之继承-5种JavaScript继承的方法
    java学习笔记之System类
    MySQL的grant,revoke使用
    LoadRunner+Android模所器录制脚本
    Mysql
    mosquitto_pub和mosquitto_sub 命令参数说明
    【转载】原 IntelliJ IDEA (idea)引入eclipse web项目
    eclipse恢复默认布局
  • 原文地址:https://www.cnblogs.com/rdchen/p/10141313.html
Copyright © 2011-2022 走看看