zoukankan      html  css  js  c++  java
  • java web项目获取项目路径

    1.方法一,获取项目运行时的真实类路径

    /* private static Logger logger = Logger.getLogger(BookController.class); */
        @RequestMapping("/index")
        public String bookHandle(HttpServletRequest servlet) {
            
            //项目路径
            String filePath3 = request.getServletContext().getRealPath("/");
            System.out.println(filePath3);
            
            return "book";
        }

    2.方法 二,获取项目运行时的真实类路径

    /* private static Logger logger = Logger.getLogger(BookController.class); */
        @RequestMapping("/index")
        public String bookHandle(HttpServletRequest servlet) {
            
            String filePath4 = BookController.class.getClassLoader().getResource("static/json/book_nav.json").getPath();
            System.out.println(filePath4);
            
            return "book";
        }

     3.用spring 获取运行时类路径路径

    String filePath = ClassUtils.getDefaultClassLoader().getResource("").getPath();

    4.其它方法

    package com.demo;
     
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
    import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
     
    import java.io.File;
     
     
    @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
    @RestController
    public class Application {
         
        public static void main(String[] args) {
            SpringApplication.run(Application.class,args);
        }
         
        @GetMapping("/lujing")
        public void getLujing() throws Exception{
            //当前项目下路径
            File file = new File("");
            String filePath = file.getCanonicalPath();
            System.out.println(filePath);
     
            //当前项目下xml文件夹
            File file1 = new File("");
            String filePath1 = file1.getCanonicalPath()+File.separator+"xml\";
            System.out.println(filePath1);
     
            //获取类加载的根路径
            File file3 = new File(this.getClass().getResource("/").getPath());
            System.out.println(file3);
     
            //获取当前类的所在工程路径
            File file4 = new File(this.getClass().getResource("").getPath());
            System.out.println(file4);
     
            //获取所有的类路径 包括jar包的路径
            System.out.println(System.getProperty("java.class.path"));
        }
    }

    转: https://www.cnblogs.com/jiangfeilong/p/11106129.html

  • 相关阅读:
    技术领导要不要写代码?
    资深程序员告诉你:如何用五年时间攒够100万?
    mfc基于对话框的应用程序,如何设置初始对话框大小,移动控件位置
    zend studio,操作记录
    xampp怎么操作数据库mysql
    mysql-font的理解
    delphi 中配置文件的使用(*.ini)和TIniFile 用法
    delphi 创建服务,安装与卸载服务
    sublime Text的一些用法(emmet插件、)
    apache (web服务器) ->php->mysql,xampp与wamp比较,WAMP与WNMP有什么区别
  • 原文地址:https://www.cnblogs.com/fps2tao/p/13274268.html
Copyright © 2011-2022 走看看