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");//关键啊 !
  • 相关阅读:
    Linux 安装apache2时候遇到的问题
    ubuntu安装php时出现error: xml2config not found. Please check your libxml2 installation 的解决办法
    Linux一块网卡绑定2个IP地址
    php 满足条件后跳转的几种方法.
    Linux 安装php 遇到 libtool: link: `ext/date/php_date.lo' is not a valid libtool object
    js 光标移动到输入框最后位置函数
    Linux iptables 防火墙相关命令介绍及使用
    tcpdump的简单选项介绍
    子网掩码转换函数
    Linux 十六进制转换十进制的函数
  • 原文地址:https://www.cnblogs.com/rdchen/p/10141313.html
Copyright © 2011-2022 走看看