spring boot在java中获取目录

时间:2024-03-12 17:12:48

//获取跟目录

String pathPrefix = ResourceUtils.getURL("classpath:").getPath();
File file = new File(pathPrefix);
if(!file.exists()) file = new File("");
System.out.println("file path:"+file.getAbsolutePath());

//获取文件可以直接如下

//File file = new File("classpath: static/images/test.png");

//如果上传目录为/static/images/upload/,则可以如下获取:
File upload = new File(file.getAbsolutePath(),"static/images/upload/");
if(!upload.exists()) upload.mkdirs();
System.out.println("upload url:"+upload.getAbsolutePath());
//在开发测试模式时,得到的地址为:{项目跟目录}/target/static/images/upload/