• 【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:documentideaProjectp-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;
        }
  • 相关阅读:
    sql命令附加数据库
    数据解析0102
    xml file parser
    c#字定义异常处理类
    simple workflow implement
    High Performance Multithreaded Work Item / Event Scheduling Engine
    字符定位
    C#实现的不重复随机数序列生成算法
    Thread并发数控制
    通过Assembly来创建Type Instance
  • 原文地址:https://www.cnblogs.com/sxdcgaq8080/p/11660950.html
Copyright © 2020-2023  润新知