在我的appconfig.xml中有:
<resource-files>
<include path="/emailtemplates/**.html" />
</resource-files>当我试图读取一个文件时,我总是得到一个FileNotFoundException。我已经尝试过了,不管有没有尾随的/,但这并没有什么区别。我还检查了这些文件是否存在于打包的应用程序中。
我的项目由模块组成,我正在windows上的本地开发系统上进行尝试。我在这里做错什么了吗?
发布于 2015-05-27 11:00:32
当你给出如此少的信息时,很难说出出了什么问题。不过,我可以给您一个例子,说明我如何使用资源。
src\main\resources\*this.getClass().getResource("/" + filename).getPath()获得完整的路径(请注意,在本例中,'/‘指的是资源所在位置的根。通常“/”指的是文件系统中的根目录或Java中的工作目录)new FileInputStream(pathFromAbove)我的pom.xml被配置为包括我的资源:
<build>
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
...发布于 2015-05-27 12:48:39
首先,我将我的资源文件夹移到了WEB文件夹中。其优点是,我不需要在appengine-web.xml中进行任何额外的配置,从而消除了一个故障点。
第二,我现在使用ServletContext获取我的资源文件的完整路径。在我使用path作为new File的参数之前,这是行不通的。我想在医生的任何地方都没有。因此,读取文本文件的代码如下所示:
StringBuilder sb = new StringBuilder();
Scanner scanner = new Scanner(new File(context.getRealPath(path)), "UTF-8");
while (scanner.hasNextLine())
sb.append(scanner.nextLine());
scanner.close();https://stackoverflow.com/questions/30475134
复制相似问题