首页 > 其他分享 >SpringBoot 中获取项目的路径和文件流

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

时间:2024-06-01 14:44:04浏览次数:18  
标签:getResource SpringBoot 路径 getPath 获取 TEST path String

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);

标签:getResource,SpringBoot,路径,getPath,获取,TEST,path,String
From: https://www.cnblogs.com/liftsail/p/18225964

相关文章

  • 基于springboot校园医疗保险管理系统(源码+lw+部署文档+讲解等)
    前言......
  • 基于springboot的毕业设计成绩管理系统源码数据库
    基于springboot的毕业设计成绩管理系统源码数据库传统办法管理信息首先需要花费的时间比较多,其次数据出错率比较高,而且对错误的数据进行更改也比较困难,最后,检索数据费事费力。因此,在计算机上安装毕业设计成绩管理系统软件来发挥其高效地信息处理的作用,可以规范信息管理流程,让......
  • Springboot 开发 -- 跨域问题技术详解
    一、跨域的概念跨域访问问题指的是在客户端浏览器中,由于安全策略的限制,不允许从一个源(域名、协议、端口)直接访问另一个源的资源。当浏览器发起一个跨域请求时,会被浏览器拦截,并阻止数据的传输。这种限制是为了保护用户的隐私和安全,防止恶意网站利用用户的浏览器向其他网站......
  • springboot 获取静态资源文件夹
    @ComponentpublicclassStaticResourcePathResolver{privatefinalServletContextservletContext;@AutowiredpublicStaticResourcePathResolver(ServletContextservletContext){this.servletContext=servletContext;}publicS......
  • 【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service
    在前一篇文章中,我们是把.NET8应用读取SSL证书(X509)示例部署在AppServiceWindows环境中,那么如果部署在Linux环境,以及LinuxContainer中呢?根据前文中的第一种方法,直接在把证书文件包含在源文件中,通过相对路径读取证书文件的方式,经测试,可以正常工作。但是,对于第二种“通过指......
  • Springboot计算机毕业设计亚洲杯志愿者管理系统小程序【附源码】开题+论文+mysql+程序
    本系统(程序+源码)带文档lw万字以上 文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容研究背景:随着各类大型活动的增多,志愿者管理成为了一个日益重要的问题。特别是在亚洲杯这样的国际性赛事中,高效的志愿者管理系统对于保障活动的顺利进行至关重......
  • Springboot计算机毕业设计牙科预约微信小程序【附源码】开题+论文+mysql+程序+部署
    本系统(程序+源码)带文档lw万字以上 文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容研究背景:随着移动互联网的普及和微信平台的广泛应用,微信小程序已成为连接线上线下的重要桥梁。在医疗健康领域,传统的牙科预约方式往往存在着效率低下、操作繁......
  • 一个进程如何获取父进程的启动命令
    原理:首先使用getppid获取父进程的ID。然后通过读取/proc/${pid}/cmdline获取指定进程的命令行。Linux似乎并没有提供直接获取cmdline的系统调用,而是在/proc/文件系统提供了这些信息。代码示例#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<u......
  • springboot3项目的搭建四.1(security登录认证配置)
    SpringBoot3整合SpringSecurityMaven<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.2.2</version><relativeP......
  • springboot3项目的搭建四.2(security登录认证配置)
    SpringBoot3+SpringSecurity整合Security导包:<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency>模拟Redis存储登录信息:publicclassCacheEntityimpl......