zoukankan      html  css  js  c++  java
  • 项目中路径问题

        public static String getPath() {
            //得到当前的classpath的绝对路径的URI表示法
            String path=Thread.currentThread().getContextClassLoader().getResource("").toString();
            //  将/换成  去掉file:   //去掉classes   去掉第一个\,如 D:JavaWeb...
            path=path.replace('/', '\').replace("file:", "").replace("classes\", "").substring(1); 
            return path;
        }
        
        //获得项目绝对路径
        public static String getProjectRootPath(){   
            String rootPath=Thread.currentThread().getContextClassLoader().getResource("").getPath();    
            rootPath = rootPath.substring(1,rootPath.indexOf("WEB-INF"));    
            return rootPath;
        }

    classpath:只能服务器才能使用。客户端不能访问这个路径下的内容。

    例如:项目名是test

    D:/apacheTomcat/apache-tomcat-7.0.59/webapps/test/WEB-INF/classes/   
    D:/apacheTomcat/apache-tomcat-7.0.59/webapps/test/ 

    @Autowired
        private HttpServletRequest request;
        @Test
        public void testUrl(){
            String schema = request.getScheme();
            String serverName = request.getServerName();
            // 端口号返回的是int类型
            int serverPort = request.getServerPort();
            String contextPath = request.getContextPath();
            String servletPath = request.getServletPath();
            System.out.println("协议:"+schema);
            System.out.println("服务器名称:"+ serverName);
            System.out.println("服务器端口号:"+ serverPort);
            System.out.println("项目名称:"+ contextPath);
            System.out.println("servlet路径:"+ servletPath);
        }

  • 相关阅读:
    操作winrar
    lucene.NET详细使用与优化详解
    js实现记住帐号或密码(js读写COOKIE)
    jQuery对select操作小结
    XMLHelper
    AJAX2用法
    as3Crypto and php, what a fun ride!
    linux文件描述符导致squid拒绝服务
    Centos 增加硬盘
    安装Squid log analyzer分析工具
  • 原文地址:https://www.cnblogs.com/bulrush/p/11114330.html
Copyright © 2011-2022 走看看