明明硬盘中存在users.xml,但是提示:系统找不到指定的路径。
解决办法:
设置String的编码格式
1 realpath = URLDecoder.decode(realpath, "UTF-8");
源代码:
1 package cn.itcast.utils; 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.UnsupportedEncodingException; 7 import java.net.URL; 8 import java.net.URLDecoder; 9 10 import org.dom4j.Document; 11 import org.dom4j.DocumentException; 12 import org.dom4j.io.OutputFormat; 13 import org.dom4j.io.SAXReader; 14 import org.dom4j.io.XMLWriter; 15 16 public class XmlUtils { 17 18 private static String filename = "users.xml"; 19 20 public static Document getDocument() throws DocumentException, UnsupportedEncodingException { 21 22 URL url = XmlUtils.class.getClassLoader().getResource(filename); 23 String realpath = url.getPath(); 24 25 realpath = URLDecoder.decode(realpath, "UTF-8"); 26 27 SAXReader reader = new SAXReader(); 28 return reader.read(new File(realpath)); 29 } 30 31 public static void write2Xml(Document document) throws IOException { 32 33 URL url = XmlUtils.class.getClassLoader().getResource(filename); 34 String realpath = url.getPath(); 35 36 realpath = URLDecoder.decode(realpath, "UTF-8"); 37 38 OutputFormat format = OutputFormat.createPrettyPrint(); 39 XMLWriter writer = new XMLWriter(new FileOutputStream(realpath), format); 40 writer.write(document); 41 writer.close(); 42 } 43 }
HTTP Status 500 – Internal Server Error
Type Exception Report
Message org.dom4j.DocumentException: C:Program%20FilesApache%20Software%20FoundationTomcat%208.5webappsday09_userWEB-INFclassesusers.xml (系统找不到指定的路径。) Nested exception: C:Program%20FilesApache%20Software%20FoundationTomcat%208.5webappsday09_userWEB-INFclassesusers.xml (系统找不到指定的路径。)
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
java.lang.RuntimeException: org.dom4j.DocumentException: C:Program%20FilesApache%20Software%20FoundationTomcat%208.5webappsday09_userWEB-INFclassesusers.xml (系统找不到指定的路径。) Nested exception: C:Program%20FilesApache%20Software%20FoundationTomcat%208.5webappsday09_userWEB-INFclassesusers.xml (系统找不到指定的路径。) cn.itcast.dao.impl.UserDaoXmlImpl.find(UserDaoXmlImpl.java:35) cn.itcast.service.impl.BusinessServiceImpl.loginUser(BusinessServiceImpl.java:29) cn.itcast.web.controller.LoginServlet.doGet(LoginServlet.java:23) cn.itcast.web.controller.LoginServlet.doPost(LoginServlet.java:38) javax.servlet.http.HttpServlet.service(HttpServlet.java:661) javax.servlet.http.HttpServlet.service(HttpServlet.java:742) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause
org.dom4j.DocumentException: C:Program%20FilesApache%20Software%20FoundationTomcat%208.5webappsday09_userWEB-INFclassesusers.xml (系统找不到指定的路径。) Nested exception: C:Program%20FilesApache%20Software%20FoundationTomcat%208.5webappsday09_userWEB-INFclassesusers.xml (系统找不到指定的路径。) org.dom4j.io.SAXReader.read(SAXReader.java:266) cn.itcast.utils.XmlUtils.getDocument(XmlUtils.java:28) cn.itcast.dao.impl.UserDaoXmlImpl.find(UserDaoXmlImpl.java:18) cn.itcast.service.impl.BusinessServiceImpl.loginUser(BusinessServiceImpl.java:29) cn.itcast.web.controller.LoginServlet.doGet(LoginServlet.java:23) cn.itcast.web.controller.LoginServlet.doPost(LoginServlet.java:38) javax.servlet.http.HttpServlet.service(HttpServlet.java:661) javax.servlet.http.HttpServlet.service(HttpServlet.java:742) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Note The full stack trace of the root cause is available in the server logs.