Java web项目如何获取某个文件夹下面的文件路径 绝对路径 ?

    /**
     * @param relativePath : 相对classpath路径
     */
    public static File getAbsoluteFileInWebContainer(String relativePath) {
        File srcFile;
        // classpath : {somedirectory}/target/classes/
        String os = System.getProperty("os.name");
        String classpath = null;
        if (os.toLowerCase().startsWith("win")) { // windows
            classpath = ConcreteFileUtil.class.getResource("/").getPath().replaceFirst("/", "");
        } else { // other
            classpath = ConcreteFileUtil.class.getResource("/").getPath();
        }
        if (!(srcFile = new File(classpath.concat(relativePath))).exists()) {
            srcFile = new File(classpath.replaceAll("WEB-INF/classes", "").concat(relativePath));
        }
        return srcFile.exists() ? srcFile : null;
    }

Example:

ConcreteFileUtil.getAbsoluteFileInWebContainer("dict/figures/three-kingdoms-figures.dict"));

你可能感兴趣的:(Java web项目如何获取某个文件夹下面的文件路径 绝对路径 ?)