SpringBoot获取文件路径

导读:本篇文章讲解 SpringBoot获取文件路径,文章出自:https://blog.csdn.net/weixin_43316702/article/details/111573812希望对大家有帮助,欢迎收藏,转发!站点地址:www.javazhiyin.com.com

>>强大,10k+点赞的 SpringBoot 后台管理系统竟然出了详细教程!

import org.junit.Test;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.IOException;

public class TestPath {

    @Test
    public void test() throws IOException {
        //获取项目下target下的路径
        String path1 = Thread.currentThread().getContextClassLoader().getResource("").getPath();
        String path2 = ResourceUtils.getURL("classpath:").getPath();
        //获取项目下的路径(加 String relate = "\\src\\main\\resources";可以得到resources下文件路径)
        String path3 = System.getProperty("user.dir");
        File directory = new File(".");
        String root = directory.getCanonicalPath();
        System.out.println(path1);
        System.out.println(path2);
        System.out.println(path3);
        System.out.println(root);
    }
}