SpringBoot 中获取项目的路径和文件流

SSM web项目

以工程名为TEST为例: 
(1)得到包含工程名的当前页面全路径:request.getRequestURI() 
结果:/TEST/test.jsp 
(2)得到工程名:request.getContextPath() 
结果:/TEST 
(3)得到当前页面所在目录下全名称:request.getServletPath() 
结果:如果页面在jsp目录下 /TEST/jsp/test.jsp 
(4)得到页面所在服务器的全路径:application.getRealPath("test.jsp") 
结果:D:\resin\webapps\TEST\test.jsp 
(5)得到页面所在服务器的绝对路径:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent(); 
结果:D:\resin\webapps\TEST 


2.在类中取得路径: 

(1)类的绝对路径:Class.class.getClass().getResource("/").getPath() 
结果:/D:/TEST/WebRoot/WEB-INF/classes/pack/ 
(2)得到工程的路径:System.getProperty("user.dir") 
结果:D:\TEST 

3.Servlet中取得路径: 

(1)得到工程目录:request.getSession().getServletContext().getRealPath("") 参数可具体到包名。 
结果:E:\Tomcat\webapps\TEST 
(2)得到IE地址栏地址:request.getRequestURL() 
结果:http://localhost:8080/TEST/test 
(3)得到相对地址:request.getRequestURI() 
结果:/TEST/test
注释:当项目中的jsp页面有<base href="<%=request.getContextPath()%>/">标签时,可以使用以下代码来获取根目录,以防项目名为空的时候报错:
 function getRootPath(){ 
return $("base").attr("href"); 
}  
var webpath=getRootPath(); //webpath就是目录路径变量 
 

SpringBoot Web项目

import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;
1、在用户头像上传的功能实现时,获取目录路径
String path = ClassUtils.getDefaultClassLoader().getResource("").getPath()
//输出path: D:/java_project/manage/target/classes/

项目中图片上传的路径是  resources/static/img/headImg/  中,路径可以这样写:
String path = ClassUtils.getDefaultClassLoader().getResource("").getPath()+"static/img/headImg/";
//输出path:/D:/java_project/manage/target/classes/static/img/headImg/

2、还有一种写法,效果一样 
//获取项目的根目录
File path = new File(ResourceUtils.getURL("classpath:").getPath());
System.out.println("path:"+path.getAbsolutePath());
//path:D:\java_project\manage\target\classes
        
//获取项目根目录下的某个文件夹,这里是  "static/img/headImg/"
File uploadpath = new File(path.getAbsolutePath(),"static/img/headImg/");
System.out.println("uploadpath:"+uploadpath.getAbsolutePath());
//uploadpath:D:\java_project\manage\target\classes\static\img\headImg

//也可以直接写成这样
String path = ResourceUtils.getURL("classpath:static/img/headImg/").getPath();
注意:ResourceUtils的这种写法在linux系统是无效,请注意

推荐使用一下两种方式:

String rootPath = Class.class.getClass().getResource("/").getPath();
//D:\java_project\manage\target\classes\

String rootPath2 = ClassUtils.getDefaultClassLoader().getResource("").getPath();
//D:\java_project\manage\target\classes\

如果获取jdk下的目录,需要指定目录文件夹获取。 ClassUtils是Spring util包下的类

public static void main(String[] args) {
    // 获取模板文件
    String rootPath = Class.class.getClass().getResource("/").getPath();
    String rootPath1 = Class.class.getClass().getResource("/jasper").getPath();
    String ctxPath = ClassUtils.getDefaultClassLoader().getResource("").getPath();
    String ctxPath1 = ClassUtils.getDefaultClassLoader().getResource("jasper").getPath();
    log.info("\n rootPath:  {}, \n rootPath1:{}", rootPath, rootPath1);
    log.info("\n ctxPath: {},\n ctxPath1:{}", ctxPath, ctxPath1);
}

在这里插入图片描述

获取resources下的文件流

1、通过ClassPathResource类获取文件流,SpringBoot中所有文件都在jar包中,没有一个实际的路径,因此可以使用以下方式。ClassPathResource、PropertiesLoaderUtils都是Spring core包下的类

	 ClassPathResource classPathResource = new ClassPathResource("jdbc.properties");
	 Properties properties = PropertiesLoaderUtils.loadProperties(classPathResource);
	 properties.list(System.out);
	 System.out.println("==============================================");
	 String property = properties.getProperty("jdbc.username");
	 System.out.println("property = " + property);

2、直接使用getResourceAsStream方法获取流,上面的几种方式都需要获取文件路径,但是在SpringBoot中所有文件都在jar包中,没有一个实际的路径,因此可以使用以下方式。

	InputStream resourceAsStream = RuoYiAuthApplication.class.getClassLoader().getResourceAsStream("jdbc.properties");
	Properties properties = new Properties();
	properties.load(resourceAsStream);
	properties.list(System.out);
	System.out.println("==============================================");
	String property = properties.getProperty("jdbc.url");
	System.out.println("property = " + property);

在这里插入图片描述

  • 13
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李熠漾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值