今日份代码:

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;
    }