Java 读取项目路径下文件

1、SpringBoot 项目

(1)、

Resource resource = new ClassPathResource("template/xixu.png");
// template 前面不要加 / 
File file = resource.getFile();

(2)、

String picPath = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "template/" + "xixu.png";

2、

String path = httpServletRequest.getSession().getServletContext().getRealPath("xixu.png");
File file = new File(path);

 

 

参考:https://blog.csdn.net/qq_36874292/article/details/85046142

posted @ 2021-10-09 09:46  唏嘘-  阅读(579)  评论(0编辑  收藏  举报