【spring】【spring boot】获取系统根路径,根目录,用于存储临时生成的文件在服务器上
今日份代码:
Spring
private static final String UPLOAD_TEMP_FILE_NAME = "测试商品数据.xlsx"; /**
* 获取临时文件路径
* @return
*/
private String getFilePath(){
String path = 当前类.class.getResource("/").getPath()+UPLOAD_TEMP_FILE_NAME;
return path;
}
Spring Boot:
//第一种
File path = new File(ResourceUtils.getURL("classpath:").getPath());
if(!path.exists()) path = new File("");
System.out.println(path.getAbsolutePath());
//第二种
System.out.println(System.getProperty("user.dir"));
//第三种
String path1 = ClassUtils.getDefaultClassLoader().getResource("").getPath();
System.out.println(URLDecoder.decode(path1, "utf-8"));
//第四种
String path2 = ResourceUtils.getURL("classpath:").getPath();
System.out.println(path2);
//第五种 spring boot打jar包,建议使用第五种
ApplicationHome h = new ApplicationHome(getClass());
File jarF = h.getSource();
System.out.println(jarF.getParentFile().toString());
//第六种 spring boot打jar包或者不打包,都可以使用的,建议区分系统以完善路径 本种也建议使用
Properties properties = System.getProperties();
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>系统:" + properties.getProperty("os.name"));
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>根路径" + properties.getProperty("user.dir")); String path = properties.getProperty("user.dir");
if (properties.getProperty("os.name").toLowerCase().contains("win")) {
path += "\\";
}else {
path += "/";
}
使用第五种示例如下:
private static final String UPLOAD_TEMP_FILE_NAME = "测试商品数据.xlsx"; /**
* 获取临时文件路径
* @return
*/
private String getFilePath(){
ApplicationHome h = new ApplicationHome(getClass());
File jarF = h.getSource();
String path = jarF.getParentFile().toString();
int i = path.lastIndexOf("/");
if (i > 0) {
path = path.substring(0,i+1);
}
path += UPLOAD_TEMP_FILE_NAME;
return path;
}
使用第六种示例如下:
windows本机未打包运行结果:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>系统:Windows 10
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>根路径E:\document\ideaProject\p-job Linux打jar包运行结果:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>系统:Linux
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>根路径/data/web/domains/p-job.com/server8097
private static final String UPLOAD_TEMP_FILE_NAME = "测试商品数据.xlsx"; /**
* 获取临时文件路径
* @return
*/
private String getFilePath(){
Properties properties = System.getProperties();
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>系统:" + properties.getProperty("os.name"));
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>根路径" + properties.getProperty("user.dir")); String path = properties.getProperty("user.dir");
if (properties.getProperty("os.name").toLowerCase().contains("win")) {
path += "\\";
}else {
path += "/";
}
path += UPLOAD_TEMP_FILE_NAME;
return path;
}
【spring】【spring boot】获取系统根路径,根目录,用于存储临时生成的文件在服务器上的更多相关文章
- Java中获取项目根路径和类加载路径的7种方法
引言 在web项目开发过程中,可能会经常遇到要获取项目根路径的情况,那接下来我就总结一下,java中获取项目根路径的7种方法,主要是通过thisClass和System,线程和request等方法. ...
- js获取项目根路径
//js获取项目根路径,如: http://localhost:8083/uimcardprj function getRootPath(){ //获取当前网址,如: http://localhost ...
- 使用GetLogicalDriveStrings获取驱动器根路径
使用GetLogicalDriveStrings获取驱动器根路径,并使用自定义的GetDriveInfo函数获取驱动器的属性. VS2012 + win7 x64下调试通过. #include < ...
- html 获取项目根路径
html 获取项目根路径 function getContextPath(){ var pathName = document.location.pathname; //当前文件的绝度路径 var i ...
- javaweb-servlet获取给定文件在服务器上的绝对路径的方法
1.通过ServletContext获取 在tomcat5,6,7版本中我们可以通过ServletContext来获取给定文件在服务器上的绝对路径. ServletContext context = ...
- Spring下获取项目根路径--good
Spring 在 org.springframework.web.util 包中提供了几个特殊用途的 Servlet 监听器,正确地使用它们可以完成一些特定需求的功能.比如某些第三方工具支持通过 ${ ...
- c# winform 获取当前程序运行根目录,winform 打开程序运行的文件夹
// 获取程序的基目录. System.AppDomain.CurrentDomain.BaseDirectory // 获取模块的完整路径. System.Diagnostics.Process.G ...
- Spring Boot 上传文件 获取项目根路径 物理地址 resttemplate上传文件
springboot部署之后无法获取项目目录的问题: 之前看到网上有提问在开发一个springboot的项目时,在项目部署的时候遇到一个问题:就是我将项目导出为jar包,然后用java -jar 运行 ...
- 【转】Spring 获取web根目录 (Spring线程获取web目录/路径/根目录,普通类获取web目录)
不使用Spring,怎样能在Listener启动的Thread中获取web目录,还真不完全确定.其实我觉得实际代码也很简单.就是基于普通的listener,然后在listener中获取web目录并放到 ...
随机推荐
- elasticsearch7 配置篇
学习了这么多,终于开始搭建生产环境了,这一篇主要讲解配置项,以及支持中文分词的ik安装,集群的搭建. 配置项确实挺多的,但把几个常用配置熟悉就好,而且就像elasticsearch官方文档所说,不存在 ...
- ios视频网盘
http://pan.baidu.com/share/home?uk=1711799154#category/type=0
- 微信小程序使用websocket通讯的demo,含前后端代码,亲测可用
目录 0.概述websocket 1.app.js写法 2.后台写法 0.概述websocket (1) 个人总结:后台设置了websocket地址,服务器开启后等待有人去连接它. 一个客户端一打开就 ...
- Ubuntu 18.04上安装Apache, MySQL, PHP, LAMP
1.安装 Apache $ sudo apt update && sudo apt install apache2 中间会遇到停顿询问是否继续, 输入 y 然后 回车. 2.测试 Ap ...
- NAT网络地址转换的原理--笔试答题版
最早接触NAT是在做网络工程师的时候,NAT是做网络工程师必需会的知识点和技能,后来在面试运维的时候也经常被用到,在运维的某些知识点当中也会被引用到,如LVS当中. 为什么需要NAT(网络地址转换)? ...
- uiautomator手动调试与快速高度设置
创建java工程:Demo1包名:com.bing.cn类名:Test测试用例:testDemo android create uitest-project -n Demo1 -t 7 -p E:\e ...
- tensorflow 待阅读的资料
tensorflow性能调优实践 https://www.jianshu.com/p/937a0ce99f56 2018.04.01 Deep Learning 之 最优化方法 https://blo ...
- python27期day16:序列化、json、pickle、hashlib、collections、软件开发规范、作业。
序列化模块:什么是序列化呢? 序列化的本质就是将一种数据结构(如字典.列表)等转换成一个特殊的序列(字符串或者bytes)的过程就叫做序列化.将这个字典直接写入文件是不可以的,必须转化成字符串的形式, ...
- (day45)JavaScript
目录 一.什么是JavaScript 二.注释 三.引入方式 (1)script标签内联 (2)script标签外联 四.变量 (一)变量声明 (二)命名规范 五.数据类型 (一)数值类型Number ...
- django之cookies,session 、中间件及跨站请求伪造
cookies 和session 为什么会有cookie? 由于http协议是无状态的,即用户访问过后,不留痕迹(状态可以理解为客户端和服务器在某次会话中产生的数据,那无状态的就以为这些数据不会被 ...