java - 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 文件位于同一目录中)。
解决方案
推荐阅读
- java - 使用以下几个队列请求-回复
- flutter - 如何为非消耗品设置应用内购买
- python - 如何从numpy数组返回索引
- javascript - 基于 Datatable Jquery 的插件 - 表格 - 表格中可折叠的问题 - Javascript/HTML
- laravel - laravel nova 观察者试图获取非对象的属性
- gitlab-ci-runner - 错误:准备失败:Getwd:getwd:没有这样的文件或目录
- python - 如何为熊猫数据框中的切片分配值
- php - 如何修复 .htaccess 错误?一直要求认证
- css - 在 CSS Grid 项目中定位元素
- excel - 提取二维数组中的重复条目