0.2---路径总结

Jsoup 的解析路径(了解便可)

//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

相关文章
相关标签/搜索