gwt servlet getRessourceAsStream 总是 returns null

gwt servlet getRessourceAsStream always returns null

我有这段代码可以在 servlet:

中加载一个文本文件
String lFileName = mServletContext.getRealPath(mFile);
InputStream lInputStream = mServletContext.getResourceAsStream(lFileName);
InputStream lInputStream2 = mServletContext.getResourceAsStream(mFile);

两个 InputStream's 都是 null。我完全不知道为什么。

mFile的值为"file.txt"lFile 的值为 "C:\development\workspace\MyGwtApp\war\file.txt".

如果我使用资源管理器导航到该目录,文件 file.txt 就在其中...!

我用 super dev mode 测试我的 gwt 应用程序。 编译 gwt 应用程序运行没有问题。

你看到问题了吗?

getResourceAsStream 定义

Finds a resource with a given name. The rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object's class loader.

这意味着您可以读取 mFile,如果它存在于您的类路径中,就像在 WEB-INF/classes 下一样。因此,将文件放在 java 类 所在的 src 目录中,然后查看文件是否进入 类 目录,然后使用其名称将其作为资源获取。示例:文件名 = "file.txt"