首页 > 解决方案 > Servlet 找不到文件

问题描述

我有一个 ServiceHandler 类,我想基本上读入一个文件。在我的 init() 函数中:

    private String languageDataSet = null; // This variable is shared by all HTTP requests for the servlet
    private static long jobNumber = 0; // The number of the task in the async queue

    private File f;

    public void init() throws ServletException {
        ServletContext ctx = getServletContext(); // Get a handle on the application context
        languageDataSet = ctx.getInitParameter("LANGUAGE_DATA_SET"); // Reads the value from the <context-param> in web.xml

        // You can start to build the subject database at this point. The init() method is only ever called once during the life cycle of a servlet
        f = new File(languageDataSet);
    }

在 doGet 函数中,我想显示有关此文件的一些信息:

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   out.print("Language Dataset is located at " + languageDataSet + " and is <b><u>" + f.length() + "</u></b> bytes in size");
}

在我的 web.xml 文件中,我有这样的上下文参数:

  <context-param>
    <param-name>LANGUAGE_DATA_SET</param-name>
    <param-value>wili-2018-Edited.txt</param-value>
  </context-param>

我得到以下输出:

Language Dataset is located at wili-2018-Edited.txt and is 0 bytes in size

它似乎找不到文件,因为我已经验证文件中有文本,因此它不应该显示为 0。知道为什么似乎找不到文件吗?(该文件与 web.xml 文件位于同一目录中)。

标签: javaxml

解决方案


推荐阅读