zoukankan      html  css  js  c++  java
  • HTTP Status 500 – Internal Server Error

    明明硬盘中存在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.


    Apache Tomcat/8.5.24

  • 相关阅读:
    【JAVA】 04-Java中的多线程
    【刷题】经典的启发式算法
    【刷题】java 常见的几种运行时异常RuntimeException
    【学习总结】认识微服务
    【刷题】知识点与易错点之简单编程思路总结
    【刷题】知识点与易错点- 总
    【刷题】知识点与易错点之数据结构
    【学习总结】《大话数据结构》- 第9章-排序
    LeetCode之“动态规划”:Valid Parentheses && Longest Valid Parentheses
    LeetCode之“动态规划”:Interleaving String
  • 原文地址:https://www.cnblogs.com/denggelin/p/8151635.html
Copyright © 2011-2022 走看看