Project Path Problem

    public  static String getPath () {
         // get the absolute path of the current classpath notation URI 
        String path = Thread.currentThread () getContextClassLoader () the getResource ( ".". ) .toString ();
         //   a / replace \ removing File:    // remove classes \ remove the first \ as \ D: \ ... JavaWeb 
        path path.replace = ( '/', '\\') Replace ( "File:", "").. Replace ( "\\ classes", "") .substring (. 1 ); 
         return path;
    }
    
    // get the absolute path project 
    public  static String getProjectRootPath () {   
        String rootPath=Thread.currentThread().getContextClassLoader().getResource("").getPath();    
        rootPath = rootPath.substring(1,rootPath.indexOf("WEB-INF"));    
        return rootPath;
    }

classpath: only server to use. The client can not access the contents of this path.

For example: The project name is 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();
        The serverName String = to request.getServerName ();
         // port number returned is of type int 
        int the serverPort = to request.getServerPort ();
        String contextPath = request.getContextPath();
        String servletPath = request.getServletPath();
        System.out.println ( "Protocol:" + Schema);
        System.out.println ( "Server Name:" + serverName);
        System.out.println ( "server port number:" + the serverPort);
        System.out.println ( "Project Name:" + contextPath);
        System.out.println("servlet路径:"+ servletPath);
    }

 


Guess you like

Origin www.cnblogs.com/bulrush/p/11114330.html