springboot之如何获取项目目录路径
作者:lss0555 发布时间:2022-08-09 21:44:17
springboot获取项目目录路径
springboot部署后获取项目的路径
//获取跟目录(绝对路径)
File path = new File(ResourceUtils.getURL("classpath:").getPath());
if(!path.exists()) path = new File("");
System.out.println("path:"+path.getAbsolutePath());
//如果上传目录为/static/images/upload/,则可以如下获取:
File upload = new File(path.getAbsolutePath(),"static/images/upload/");
if(!upload.exists()) upload.mkdirs();
System.out.println("upload url:"+upload.getAbsolutePath());
//在开发测试模式时,得到的地址为:{项目跟目录}/target/static/images/upload/
//在打包成jar正式发布时,得到的地址为:{发布jar包目录}/static/images/upload/
springboot获取resources目录资源文件9种方式
本文中提供了九种方式获取resources
目录下文件的方式。
其中打印文件的方法如下:
/**
* 根据文件路径读取文件内容
*
* @param fileInPath
* @throws IOException
*/
public static void getFileContent(Object fileInPath) throws IOException {
BufferedReader br = null;
if (fileInPath == null) {
return;
}
if (fileInPath instanceof String) {
br = new BufferedReader(new FileReader(new File((String) fileInPath)));
} else if (fileInPath instanceof InputStream) {
br = new BufferedReader(new InputStreamReader((InputStream) fileInPath));
}
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
方式一
主要核心方法是使用getResource
和getPath
方法,这里的getResource("")
里面是空字符串
public void function1(String fileName) throws IOException {
String path = this.getClass().getClassLoader().getResource("").getPath();//注意getResource("")里面是空字符串
System.out.println(path);
String filePath = path + fileName;
System.out.println(filePath);
getFileContent(filePath);
}
方式二
主要核心方法是使用getResource
和getPath
方法,直接通过getResource(fileName)
方法获取文件路径,注意如果是路径中带有中文一定要使用URLDecoder.decode解码。
/**
* 直接通过文件名getPath来获取路径
*
* @param fileName
* @throws IOException
*/
public void function2(String fileName) throws IOException {
String path = this.getClass().getClassLoader().getResource(fileName).getPath();//注意getResource("")里面是空字符串
System.out.println(path);
String filePath = URLDecoder.decode(path, "UTF-8");//如果路径中带有中文会被URLEncoder,因此这里需要解码
System.out.println(filePath);
getFileContent(filePath);
}
方式三
直接通过文件名+getFile()来获取文件。如果是文件路径的话getFile和getPath效果是一样的,如果是URL路径的话getPath是带有参数的路径。
如下所示:
url.getFile()=/pub/files/foobar.txt?id=123456
url.getPath()=/pub/files/foobar.txt
使用getFile()方式获取文件的代码如下:
/**
* 直接通过文件名+getFile()来获取
*
* @param fileName
* @throws IOException
*/
public void function3(String fileName) throws IOException {
String path = this.getClass().getClassLoader().getResource(fileName).getFile();//注意getResource("")里面是空字符串
System.out.println(path);
String filePath = URLDecoder.decode(path, "UTF-8");//如果路径中带有中文会被URLEncoder,因此这里需要解码
System.out.println(filePath);
getFileContent(filePath);
}
方式四(重要)
直接使用getResourceAsStream
方法获取流,上面的几种方式都需要获取文件路径,但是在SpringBoot中所有文件都在jar包中,没有一个实际的路径,因此可以使用以下方式。
/**
* 直接使用getResourceAsStream方法获取流
* springboot项目中需要使用此种方法,因为jar包中没有一个实际的路径存放文件
*
* @param fileName
* @throws IOException
*/
public void function4(String fileName) throws IOException {
InputStream in = this.getClass().getClassLoader().getResourceAsStream(fileName);
getFileContent(in);
}
方式五(重要)
主要也是使用getResourceAsStream
方法获取流,不使用getClassLoader
可以使用getResourceAsStream("/配置测试.txt")
直接从resources根路径下获取,SpringBoot中所有文件都在jar包中,没有一个实际的路径,因此可以使用以下方式。
/**
* 直接使用getResourceAsStream方法获取流
* 如果不使用getClassLoader,可以使用getResourceAsStream("/配置测试.txt")直接从resources根路径下获取
*
* @param fileName
* @throws IOException
*/
public void function5(String fileName) throws IOException {
InputStream in = this.getClass().getResourceAsStream("/" + fileName);
getFileContent(in);
}
方式六(重要)
通过ClassPathResource
类获取文件流,SpringBoot中所有文件都在jar包中,没有一个实际的路径,因此可以使用以下方式。
/**
* 通过ClassPathResource类获取,建议SpringBoot中使用
* springboot项目中需要使用此种方法,因为jar包中没有一个实际的路径存放文件
*
* @param fileName
* @throws IOException
*/
public void function6(String fileName) throws IOException {
ClassPathResource classPathResource = new ClassPathResource(fileName);
InputStream inputStream = classPathResource.getInputStream();
getFileContent(inputStream);
}
方式七
通过绝对路径获取项目中文件的位置,只是本地绝对路径,不能用于服务器获取。
/**
* 通过绝对路径获取项目中文件的位置(不能用于服务器)
* @param fileName
* @throws IOException
*/
public void function7(String fileName) throws IOException {
String rootPath = System.getProperty("user.dir");//E:\WorkSpace\Git\spring-framework-learning-example
String filePath = rootPath + "\\chapter-2-springmvc-quickstart\\src\\main\\resources\\"+fileName;
getFileContent(filePath);
}
方式八
通过new File("")
获取当前的绝对路径,只是本地绝对路径,不能用于服务器获取。
/**
* 通过绝对路径获取项目中文件的位置(不能用于服务器)
* @param fileName
* @throws IOException
*/
public void function8(String fileName) throws IOException {
//参数为空
File directory = new File("");
//规范路径:getCanonicalPath() 方法返回绝对路径,会把 ..\ 、.\ 这样的符号解析掉
String rootCanonicalPath = directory.getCanonicalPath();
//绝对路径:getAbsolutePath() 方法返回文件的绝对路径,如果构造的时候是全路径就直接返回全路径,如果构造时是相对路径,就返回当前目录的路径 + 构造 File 对象时的路径
String rootAbsolutePath =directory.getAbsolutePath();
System.out.println(rootCanonicalPath);
System.out.println(rootAbsolutePath);
String filePath = rootCanonicalPath + "\\chapter-2-springmvc-quickstart\\src\\main\\resources\\"+fileName;
getFileContent(filePath);
}
方式九
主要是通过设置环境变量,将文件放在环境变量中,原理也是通过绝对路径获取。
示例中我设置了一个环境变量:TEST_ROOT=E:\\WorkSpace\\Git\\spring-framework-learning-example
System.getenv("TEST_ROOT");
System.getProperty("TEST_ROOT")
通过设置环境变量的方式,然后通过绝对路径获取文件
/**
* 通过绝对路径获取项目中文件的位置
*
* @param fileName
* @throws IOException
*/
public void function9(String fileName) throws IOException {
System.setProperty("TEST_ROOT","E:\\WorkSpace\\Git\\spring-framework-learning-example");
//参数为空
String rootPath = System.getProperty("TEST_ROOT");
System.out.println(rootPath);
String filePath = rootPath + "\\chapter-2-springmvc-quickstart\\src\\main\\resources\\"+fileName;
getFileContent(filePath);
}
来源:https://lss0555.blog.csdn.net/article/details/83855890


猜你喜欢
- 在Servlet2.5中,我们要实现文件上传功能时,一般都需要借助第三方开源组件,例如Apache的commons-fileupload组件
- 目录知识点介绍正文1、质量压缩2、采样率压缩3、缩放法压缩4、RGB_565 通过改变图片格式来实现压缩总结知识点介绍Android 中图片
- Android中ListView下拉刷新实现效果图:ListView中的下拉刷新是非常常见的,也是经常使用的,看到有很多同学想要,那我就整理
- 配置文件-yaml在spring Boot开发中推荐使用yaml来作为配置文件。基本语法:key: value;kv之间有空格大小写敏感使用
- 消息发送过程消息的发送可能会经过 * 、序列化、分区器等过程。消息发送的主要涉及两个线程,分别为main线程和sender线程。如图所示,主
- 本文实例讲述了Android学习笔记之应用单元测试。分享给大家供大家参考,具体如下:第一步:在AndroidManifest.xml中加入如
- 一、引言大家都知道单例模式,通过一个全局变量来避免重复创建对象而产生的消耗,若系统存在大量的相似对象时,又该如何处理?参照单例模式,可通过对
- 在新版的AndroidStudio3.6 中,在项目的包下新建 activity 时,一般会同时生成对应的java和xml文件,例如新建 M
- 前言Java线程池中提交任务运行,通常使用execute()方法就足够了。那如果想要实现在主线程中阻塞获取线程池任务运行的结果,该怎么办呢?
- Android CheckBox中设置padding无效解决办法CheckBox使用本地图片资源CheckBox是Android中用的比较多
- springboot跳转到指定页面controller的写法必须是templates下面的页面,不经过配置,无法直接跳转到public,st
- 一、前言SQLite是一个轻量级的、跨平台的、开源的嵌入式数据库引擎,也是一个关系型的的使用SQL语句的数据库引擎,读写效率高、资源消耗总量
- 本文实例为大家分享了Java实现在线聊天室的具体代码,供大家参考,具体内容如下- 本文讲述了从实现单个多个客户的收发信息(基础简易版),到各
- Random类介绍Random类一个用于产生 伪随机 数字的类。这里的伪随机表示有随机性但是可以基于算法模拟出随机规律。Random类的构造
- public void CutToF(Stream stream)
- 一、@EnableTransactionManagement工作原理开启Spring事务本质上就是增加了一个Advisor,但我们使用 @E
- 本文实例讲述了JAVA获取任意http网页源代码。分享给大家供大家参考,具体如下:JAVA获取任意http网页源代码可实现如下功能:1. 获
- 简介本文主要讲解java 利用Selenium 操作浏览器网站时候,需要用的js的地方,代码该如何实现。调用JavaScriptwebdri
- 我们知道在编程时许多操作(如更新UI)需要在主线程中完成,而且,耗时操作(如网络连接)需要放在子线程中,否则会引起ANR。所以我们常使用Ha
- 说到网络,相信大家都对TCP、UDP和HTTP协议这些都不是很陌生,学习这部分应该先对端口、Ip地址这些基础知识有一定了解,后面我们都是直接