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

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. 带有IBM大脑的浮动机器人被成功引导至太空

    近日,带有IBM大脑的浮动机器人被成功引导至太空,在接下来的装运前往国际空间站包近三吨的研究和再补给材料. 机器人的全名是CrewInteractiveMobileCompanion:Cimon.它看 ...

  2. python关于window文件写入后,换行默认\r\n的问题

    因为python兼容各种平台,所以当在window打开文本文件写入后,换行会默认写成\r\n linux是\n 如果想去掉换行的\r 解决方法:在open函数里写入换行要求即可 with open(f ...

  3. 刷题or源码链接

    Hadoop权威指南的Github https://github.com/tomwhite/hadoop-book hadoopAPI http://hadoop.apache.org/docs/cu ...

  4. STM32F103系列命名规则

    对于STM32F103xxyy系列:第一个x代表引脚数:T-36pin,C-48pin,R-64pin,V-100pin,Z-144pin:第二个x代表Flash容量:6-32K,8-64K,B-12 ...

  5. 通俗理解BiLSTM-CRF命名实体识别模型中的CRF层

    虽然网上的文章对BiLSTM-CRF模型介绍的文章有很多,但是一般对CRF层的解读比较少. 于是决定,写一系列专门用来解读BiLSTM-CRF模型中的CRF层的文章. 我是用英文写的,发表在了gith ...

  6. Lock之ReentrantLock及实现生产者消费者和死锁

    Lock是顶层接口,它的实现逻辑并未用到synchronized,而是利用了volatile的可见性.ReentrantLock对了Lock接口的实现主要依赖了Sync,而Sync继承了 Abstra ...

  7. LDD快速参考

    第二章 快速参考 本节中出现的条目会以它们在文中出现的顺序列出: insmod modprobe rmmod 用来装载模块到正运行的内核和移除模块的用户空间工具: #include <linux ...

  8. 「长乐集训 2017 Day8」修路 (斯坦纳树)

    题目描述 村子间的小路年久失修,为了保障村子之间的往来,AAA君决定带领大家修路. 村子可以看做是一个边带权的无向图GGG, GGG 由 nnn 个点与 mmm 条边组成,图中的点从 1∼n1 \si ...

  9. hdu 5120 Intersection (圆环面积相交->圆面积相交)

    Problem Description Matt is a big fan of logo design. Recently he falls in love with logo made up by ...

  10. 【MySQL】selectKey获取insert后的自动主键

    <insert id="insert" parameterType="cc.mrbird.febs.energy.domain.ChatGroup"> ...