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外的文件

ServletContext是从out项目的根路径开始寻找的

猜你喜欢

转载自www.cnblogs.com/dilidiliBang/p/11742109.html