//1.獲取student.xml的path String path = JsoupDemo6.class.getClassLoader().getResource("student.xml").getPath(); System.out.println(path); // /D:/develop/JetBrains/IdeaProjects/java_EE-htm/day_10-12-JS&bootstrap&xml/out/production/day12_xml/student.xml //2.獲取Document對象 /* File file = new File("student.xml"); //這裏是由於用了class的方法,因此不能再變異以前用方法,只能用類加載器 System.out.println(file.getAbsolutePath());*/ // D:\develop\JetBrains\IdeaProjects\java_EE-htm\day_10-12-JS&bootstrap&xml\student.xml Document document = Jsoup.parse(new File(path), "utf-8"); 這裏是解析,因此是要絕對路徑。
// 獲取文件的服務器路徑 String b = context.getRealPath("/b.txt");//web目錄下資源訪問 System.out.println(b); D:\develop\JetBrains\IdeaProjects\java_EE_web\day13-day16\out\artifacts\day15_response_war_exploded\b.txt String path = ServletContextDemo5.class.getClassLoader().getResource("a.txt").getPath(); System.out.println(path); /D:/develop/JetBrains/IdeaProjects/java_EE_web/day13-day16/out/artifacts/day15_response_war_exploded/WEB-INF/classes/a.txt
類加載器是從 ,原 src下,現classes下 開始獲取文件的絕對路徑的,不能得到原src外的文件java
ServletContext是從out項目的根路徑開始尋找的web