今日份代码:

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】获取系统根路径,根目录,用于存储临时生成的文件在服务器上的更多相关文章

  1. Java中获取项目根路径和类加载路径的7种方法

    引言 在web项目开发过程中,可能会经常遇到要获取项目根路径的情况,那接下来我就总结一下,java中获取项目根路径的7种方法,主要是通过thisClass和System,线程和request等方法. ...

  2. js获取项目根路径

    //js获取项目根路径,如: http://localhost:8083/uimcardprj function getRootPath(){ //获取当前网址,如: http://localhost ...

  3. 使用GetLogicalDriveStrings获取驱动器根路径

    使用GetLogicalDriveStrings获取驱动器根路径,并使用自定义的GetDriveInfo函数获取驱动器的属性. VS2012 + win7 x64下调试通过. #include < ...

  4. html 获取项目根路径

    html 获取项目根路径 function getContextPath(){ var pathName = document.location.pathname; //当前文件的绝度路径 var i ...

  5. javaweb-servlet获取给定文件在服务器上的绝对路径的方法

    1.通过ServletContext获取 在tomcat5,6,7版本中我们可以通过ServletContext来获取给定文件在服务器上的绝对路径. ServletContext context = ...

  6. Spring下获取项目根路径--good

    Spring 在 org.springframework.web.util 包中提供了几个特殊用途的 Servlet 监听器,正确地使用它们可以完成一些特定需求的功能.比如某些第三方工具支持通过 ${ ...

  7. c# winform 获取当前程序运行根目录,winform 打开程序运行的文件夹

    // 获取程序的基目录. System.AppDomain.CurrentDomain.BaseDirectory // 获取模块的完整路径. System.Diagnostics.Process.G ...

  8. Spring Boot 上传文件 获取项目根路径 物理地址 resttemplate上传文件

    springboot部署之后无法获取项目目录的问题: 之前看到网上有提问在开发一个springboot的项目时,在项目部署的时候遇到一个问题:就是我将项目导出为jar包,然后用java -jar 运行 ...

  9. 【转】Spring 获取web根目录 (Spring线程获取web目录/路径/根目录,普通类获取web目录)

    不使用Spring,怎样能在Listener启动的Thread中获取web目录,还真不完全确定.其实我觉得实际代码也很简单.就是基于普通的listener,然后在listener中获取web目录并放到 ...

随机推荐

  1. 第3篇-超市管理系统Scrum冲刺博客

    一.站立式会议: 1.会议照片 2.昨天完成的工作 ①数据库方面:根据需求关系为在数据库中建立相关表的基本模型供后续参考. ②前端方面:完成了登录界面的设计:各个界面的草图:为各个界面选取合适的图片如 ...

  2. Writing Your Own Widget(自定义组件)

    英文地址:http://dojotoolkit.org/reference-guide/1.10/quickstart/writingWidgets.html#quickstart-writingwi ...

  3. win7 安装vb6

    1. 用setup.exe有问题,用acmsetup.exe 2.打开setupwiz.ini,把"acme=acmboot.exe"改为"=setup\acmsetup ...

  4. 在表单中使用ajax,成功后跳转指定页面 出现Provisional headers are shown 解决办法

    问题回顾: 在表单里面,有个button按钮,在点击这个button的时候,我发送了ajax请求,然后请求成功的话,就使用window.location.href = xxx,跳转到其他页面 但是,一 ...

  5. hydra使用,实例介绍

    hydra 是一个网络帐号破解工具,支持多种协议.其作者是van Hauser,David Maciejak与其共同维护.hydra在所有支持GCC的平台能很好的编译,包括Linux,所有版本的BSD ...

  6. Win平台下窗口操作(Unity)

    Unity发布在Win平台时, 可以通过user32.dll的方式与Win API通信, 最小化窗口代码: public static class DllImports { private const ...

  7. android主流开源自动化框架(monkeyrunner,robotium,uiautomator)转载

    摘要: android自动化框架小结:monkey,monkeyrunner,cts,robotium,uiautomator android自动化框架: Uiautomator: 优点:可以对所有操 ...

  8. Exception in createBlockOutputStream

    Exception in createBlockOutputStream 出现这个问题,可能是端口没打开,把异常往下拉,就可以看到哪个端口,在centos 打开端口

  9. Taro/微信小程序解析XML

    npm i xmldom PS: https://www.npmjs.com/package/xmldom https://blog.csdn.net/caojie1008/article/detai ...

  10. [RN] 使用 Genymotion 导致 ” Genymotion 已连接,但无法访问互联网 “ 的错误

    使用 Genymotion 导致 Genymotion 已连接,但无法访问互联网 的错误 先把要点 放前面: 网络二 一定要设置 桥接模式 网上很多文章都是设置为 NAT,笔者均失败! 笔者使用的An ...