A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

1、手动拼接获取项目的绝对路径   

/**

   * 得到文件保存的路径

   *

    * @return

   */

public String getPath() {

      System.out.println("*********");

      String path1 = Thread.currentThread().getContextClassLoader().getResource("").getPath();//获取当前资源的虚拟路径

      System.out.println(path1);

      String currentProjectName = this.request.getContextPath();//获取当前项目名称

      System.out.println(this.request.getContextPath());

      int num = path1.indexOf(".metadata");

      String testPath = path1.substring(1, num).replace('/', '\\') +currentProjectName + "\\" + this.getDirectory()

           + "\\";

      System.out.println(testPath);

      File file = new File(testPath);

//判断该路径下文件是否存在,不存在则创建

      if (!file.exists()) {

        file.mkdirs();

      }

      return testPath;

}

输出结果为:/reflectWebD:\code\studyCode\/reflectWeb\upload\D:\code\studyCode\/reflectWeb\upload\fe6156bb-932f-41ad-9a1c-b20cd8ac06e5.jpg**********2、其他获取资源的路径的方法

String path = super.getServletContext().getRealPath("/") + File.separator+ this.getDirectory()

           + File.separator;

      System.out.println("#####获取当前classpath的绝对url路径####");

      System.out.println(this.getClass().getClassLoader().getResource("").getPath());

      System.out.println(this.getClass().getClassLoader().getResource("/").getPath());

      System.out.println("#####获取当前类的加载目录,如果有“/”获取当前类的所在工程路径####");

      System.out.println(this.getClass().getResource("").getPath());

      System.out.println(this.getClass().getResource("/").getPath());

      System.out.println("#####项目绝对路径####");

      System.out.println(this.getClass().getClassLoader().getResource(".").getPath());

      System.out.println("#####当前文件路径####");

      System.out.println(this.getServletContext().getRealPath("/"));

      System.out.println(super.getServletContext().getRealPath("/"));

      System.out.println(path);

输出结果为:
  • #####获取当前classpath的绝对url路径####
  • /D:/code/studyCode/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/reflectWeb/WEB-INF/classes/
  • /D:/code/studyCode/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/reflectWeb/WEB-INF/classes/
  • #####获取当前类的加载目录,如果有“/”获取当前类的所在工程路径####
  • /D:/code/studyCode/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/reflectWeb/WEB-INF/classes/cn/mldn/servlet/
  • /D:/code/studyCode/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/reflectWeb/WEB-INF/classes/
  • #####项目绝对路径####
  • /D:/code/studyCode/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/reflectWeb/WEB-INF/classes/
  • #####当前文件路径####
  • D:\code\studyCode\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\reflectWeb\
  • D:\code\studyCode\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\reflectWeb\
  • D:\code\studyCode\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\reflectWeb\\upload\
  • *********

5 个回复

正序浏览
回复 使用道具 举报
回复 使用道具 举报
虽然还没学到,先提前关注一波
来自宇宙超级黑马专属安卓客户端来自宇宙超级黑马专属安卓客户端
回复 使用道具 举报
优秀,奈斯
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马