【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目录并放到 ...
随机推荐
- uni-app学习(六)好用的插件4
1. uni-app学习(六)好用的插件4 1.1. QQ音乐模板 点击这里 示例 1.2. 画廊(ynGallery)组件 点击这里 看起来不错的 示例 1.3. 评价模板 学到个动画用法 imag ...
- redis 配置及编写启动脚本
#!/bin/sh # # Simple Redis init.d script conceived to work on Linux systems # as it does use of the ...
- vue学习指南:第十一篇(详细) - Vue的 路由 第一篇 ( router )
一.路由的配置 路由 vue-router 1. 什么是路由? 路由相当于一个配置对象 路由:就是我们通过不同的url访问不同的内容,通过angular.js 可以实现多视图的单页,现在流行的单页面 ...
- odoo10学习笔记十一:视图综述
转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/11189322.html 一:视图标签等公共结构 name (必选) 用于通过名字查找标签 model: 与v ...
- 4-1 Matplotlib 概述
Matplotlib概述 In [1]: import numpy as np import matplotlib.pyplot as plt #pyplot是matplotlib的画图的接口 ...
- openstack Train版 “nova-status upgrade check”报错:Forbidden: Forbidden (HTTP 403)
部署openstack train版,在部署完nova项目时,进行检查,执行 nova-status upgrade check 返回报错信息如下: [root@controller ~]# nova ...
- pdfium
https://github.com/SubtleCow/AccessControlListsintheDOM/tree/4673d995e5614bc682cecd22f9b2919b2360273 ...
- Centos7 安装mysql-8.0.18(rpm)
1.前言 当前MySQL最新版本:8.0.18 (听说比5.7快2倍)官方之前表示:MySQL 8.0 正式版 8.0.18 已发布,MySQL 8 要比 MySQL 5.7 快 2 倍,还带来了大量 ...
- csp考前
T1不会太麻烦,不行心里多说几遍"沙比提,沙比提".就做出来了. 后天就要出发了,可是我感觉不到长进---- 可能又学一年是个不明智的想法,退役预定.
- DP问题(3) : hdu 1080
题目转自hdu 1080,题目传送门 题目大意: 不想翻译! 解题思路: 其实就是一道变异的求lcs(Longest common subsequence 最长公共子序列)的题 不过,它的依据是下面这 ...