1.如果是在纯java类中
String dirpath = System.getProperty("user.dir");
String xmlFile = dirpath + "/WebRoot/WEB-INF/server.xml";
String fileName = dirPath + "/server.xml";
2.在servlet中
String dirPath = getServletContext().getRealPath( "/WEB-INF");
String xmlFile = dirpath + "/server.xml";
3.在jsp中
String dirPath= request.getServletContext().getRealPath("/WEB-INF");
String xmlFile = dirPath+"server.xml";
xml文件是放在WebRoot/WEB_INF下
报错:java.net.MalformedURLException: unknown protocol: d原因是:
我们需要在一个本地磁盘路径前面加上 file:// 来表示 URL 得到一个 file://d:/abc,不能直接 D:/abc 这种写法。
错误就在于直接用 d:/abc 当 URL,所以它报告错误说不认识 d 这种协议。
String url =
"file://"
+
xmlFile ;