注意:有时获取到的项目路径后再+“自定义路径后” 路径不可用,这时要看下项目里自定义路径是不是空文件夹,如果是空文件夹则调试和运行时文件夹不会编译到部署文件里.

1.方法一

调试时只能获取eclipse 项目未编译前的路径 不太好用

    /* private static Logger logger = Logger.getLogger(BookController.class); */
@RequestMapping("/index")
public String bookHandle(HttpServletRequest servlet) { JSONObject json = JsonResourceUtils.getJsonObjFromResource
("static/json/book_nav.json",servlet.getServletContext().getRealPath("/")); return "book";
}

2.方法 二

获取项目运行时的真实类路径

/* private static Logger logger = Logger.getLogger(BookController.class); */
@RequestMapping("/index")
public String bookHandle(HttpServletRequest servlet) { JSONObject json = JsonResourceUtils.getJsonObjFromResource
        /* 这里直接获取到了文件路径 */
(BookController.class.getClassLoader().getResource("static/json/book_nav.json").getPath()); 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"));
}
}

java web项目获取项目路径的更多相关文章

  1. java web中获取各种路径

    一.获取项目路径: 1) String path = request.getContextPath() :// /TestMyEclipseString basePath = request.getS ...

  2. Java中动态获取项目根目录的绝对路径

    https://www.cnblogs.com/zhouqing/archive/2012/11/10/2757774.html 序言 在开发过程中经常会用到读写文件,其中就必然涉及路径问题.使用固定 ...

  3. Weblogic部署web项目获取项目根目录为null

    写在前面 图片上传功能, web项目部署在本地Tomcat上并没有问题, 但是打成war包部署到Linux服务器Weblogic下却出现如题问题, 导致图片上传失败. 问题代码 String real ...

  4. java web 手动部署项目步骤

    java Web 手动部署项目步骤 1 在tomcat下面的webapps下面建立需要部署的文件夹(eg:demo);2 在demo下建立 WEB-INF WETA-INF src 文件夹;3 在sr ...

  5. java web项目获取各种路径

    1.可以在servlet的init方法里 String path = getServletContext().getRealPath("/"); 这将获取web项目的全路径 例如 ...

  6. java项目获取根路径(web项目和application项目的区分)

    Java项目中经常要读取配置文件,涉及到读取配置文件的地方,就会要读定位文件的路径.因此,在项目如何正确获取文件路径尤为关键. 根据不同的java项目,在获取文件路径时候有一些 小区别 测试环境:E: ...

  7. JAVA类中获取项目路径

    在java web项目中获取项目的src/main/resource下的文件路径 当前类名.class.getClassLoader().getResource("/").getP ...

  8. java 或者 js 获取项目访问路径(域名)

    /** * 获得站点url * @return */ public String getWebUrl(){ String url = getRequest().getScheme() + " ...

  9. Servlet & JSP - Java Web 访问资源的路径问题

    假设 Web 工程的目录结构如下图所示,并且 HelloServlet 配置为 @WebServlet(name = "helloServlet", urlPatterns = { ...

随机推荐

  1. Random 生成随机数

    Random类 (java.util) Random类中实现的随机算法是伪随机,也就是有规则的随机.在进行随机时,随机算法的起源数字称为种子数(seed),在种子数的基础上进行一定的变换,从而产生需要 ...

  2. Linux中的uniq命令(去掉重复项,输出重复项)

    ls /bin /usr/bin | sort | uniq | less 上面这条命令的实际效果是: 获得 ls /bin /usr/bin 的 output 将上述 output 进行 sort ...

  3. 51nod 1714:B君的游戏(博弈 sg打表)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1714 nim游戏的一个变形,需要打出sg函数的表 #incl ...

  4. Halo(十三)

    Spring Boot Actuator 请求跟踪 Spring Boot Actuator 的关键特性是在应用程序里提供众多 Web 接口, 通过它们了解应用程序运行时的内部状况,且能监控和度量 S ...

  5. 微信小程序上拉加载下拉刷新

    微信小程序实现上拉加载下拉刷新 使用小程序默认提供方法. (1). 在xxx.json 中开启下拉刷新,需要设置backgroundColor,或者是backgroundTextStyle ,因为加载 ...

  6. [原创] Delphi Create(Application) 和 Create(nil) 的区别

    Delphi Create(Application) 和 Create(nil) 的区别: 例如: Form1:=TForm1.Create(Application); Form1:=TForm1.C ...

  7. CDH6.3.1安装hue 报错

    x 一.查看日志server运行日志 /var/log/cloudera-scm-server/cloudera-scm-server.log 2019-12-11 17:28:34,201 INFO ...

  8. chromedriver与chrome版本映射表(更新至v2.46)

    chromedriver版本 支持的Chrome版本 v2.46 v71-73 v2.45 v70-72 v2.44 v69-71 v2.43 v69-71 v2.42 v68-70 v2.41 v6 ...

  9. 2018-2019-2 20175126谢文航 实验三《敏捷开发与XP实践》实验报告

    一.实验报告封面 课程:Java程序设计 班级:1751 班 姓名:谢文航 学号:20175126 指导教师:娄嘉鹏 实验日期:2019年5月2日 实验时间:--- 实验序号:实验三 实验名称:敏捷开 ...

  10. 8051 r0-r7 是什么

    R0~R7表示当前选中的寄存器组的寄存器0~7,5I机有p0,p1,p2,p3口,每组有八个寄存器(R0-R7),比如MOV A Rn (n=0~7),表示把寄存器Rn 的内容送给累加器A,其中源操作 ...