【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目录并放到 ...
随机推荐
- C++智能指针解析
前言 在C++程序中,内存分为三种静态内存.栈内存.堆内存.其中静态内存和栈内存由系统进行维护,而堆内存则是由程序员自己进行维护,也就是我们在new和delete对象时,这些对象存放的区域.任何有C+ ...
- Python从零开始——集合Set
一:Python集合知识概览 二:Python的特性.格式.以及各序列结构对比 三:Python集合set的创建 四:集合常用操作之——添加元素 五:集合常见操作之——删除元素 六:集合常见操作之—— ...
- Highways POJ - 1751
题目链接:https://vjudge.net/problem/POJ-1751 思路: 最小生成树板子,只需要多记录每个dis[x]的权值是从哪个点到x这个点的. #include <stdi ...
- torchline:让Pytorch使用的更加顺滑
torchline地址:https://github.com/marsggbo/torchline 相信大家平时在使用Pytorch搭建网络时,多少还是会觉得繁琐,因为我们需要搭建数据读取,模型,训练 ...
- Ubuntu下搭建Kubernetes集群(3)--k8s部署
1. 关闭swap并关闭防火墙 首先,我们需要先关闭swap和防火墙,否则在安装Kubernetes时会导致不成功: # 临时关闭 swapoff -a # 编辑/etc/fstab,注释掉包含swa ...
- 修改woocommerce列表产品显示数量
WooCommerce列表产品数量默认显示为10,如果是显示3列或4列,则最后一行会有空白留出,为了美观,往往我们要设置显示合适的产品数量.因此,只要把如下代码复制到当前主题的functions.ph ...
- Location配置与ReWrite语法(五)
原文链接:https://www.cnblogs.com/crazylqy/p/6892010.html location表示uri方式定位,基础语法有三种: location = pattern { ...
- 反射&异常
反射 通过字符串映射或修改程序运行时的状态.属性.方法.python中的反射功能是由以下四个内置函数提供:hasattr.getattr.setattr.delattr,改四个函数分别用于对对象内部执 ...
- P256 VRF实现解读
目录 P256 VRF实现及其改造 公式推导 H1:把任意信息映射到曲线上的点 H2: 映射任意信息为(1,q) 计算随机数 随机数的proof 如何验证 VRF优点 针对S256曲线的改造 1. 使 ...
- [POJ2336]Ferry Loading II
题目描述 Description Before bridges were common, ferries were used to transport cars across rivers. Rive ...