在多模块 Google AppEngine 中获取资源文件
Get resource file in multi-module Google AppEngine
我正在关注这个文档:
https://cloud.google.com/appengine/docs/java/config/appconfig#Static_Files_and_Resource_Files
像这样设置特定的资源文件位置:
appengine-web.xml:
<resource-files>
<include path="/resources/*" />
</resource-files>
现在,我需要在 servlet 上下文之外获取资源文件。所以,我试图通过以下方式获取文件:
InputStream jsonResource = new FileInputStream("default/WEB-INF/resources/" + resourceName);
这在本地开发服务器上运行良好,但在生产中,我得到一个 IOException:
getResourceAsString: /base/data/home/apps/s~my-app-id/module2:multi-module-1.384597049666152751/default/WEB-INF/resources/query_url.json (No such file or directory)
这是我的 GAE 模块结构:
root-ear
- default
- src/main/webapp/WEB-INF
- resources
- src/main/java (some code)
- module2
- src/main/java (code that needs the resource)
我看错文件了吗?资源文件的正确路径应该是什么?
其他详情:
我刚刚从单个模块 AppEngine 应用程序迁移 - 仅使用 classLoader.getResourceFromStream 就足以从 src/main/resources 获取我的资源,但这不再适用于耳朵结构.
我正在使用 Gradle 作为我的配置,如 appengine 示例中所建议的:
https://github.com/GoogleCloudPlatform/appengine-modules-sample-java
我无法使用 ServletContext.getResources,因为我无权访问需要这些资源文件的服务中的 "servlet context"。 (虽然我可以一直传递它,如果它是绝对需要的 - 但根据文档,如果我正确知道路径,听起来 java.io.File 应该能够为我获取资源文件)
如果有人可以在我的 "build.gradle" 中建议正确的设置,将资源放在 gradle-appengine-plugin 将拾取并放置它的地方在 GAE 产品的当前路径中,这将是一个完美的解决方案!
当前目录包含"WEB-INF"和“_ah”文件夹。
以下是 WEB-INF 中的结构:
WEB-INF
- <all my files from my normal WEB-INF>
- resources <<< The folder I added under <resource-files>
- lib
- classes
- appengine-generated
我能够通过使用以下代码段列出本地文件来解决这个问题:
File curDir = new File("./WEB-INF");
File[] files = curDir.listFiles();
for (File file: files) {
logger.info(file.getName());
}
我正在关注这个文档: https://cloud.google.com/appengine/docs/java/config/appconfig#Static_Files_and_Resource_Files 像这样设置特定的资源文件位置:
appengine-web.xml:
<resource-files>
<include path="/resources/*" />
</resource-files>
现在,我需要在 servlet 上下文之外获取资源文件。所以,我试图通过以下方式获取文件:
InputStream jsonResource = new FileInputStream("default/WEB-INF/resources/" + resourceName);
这在本地开发服务器上运行良好,但在生产中,我得到一个 IOException:
getResourceAsString: /base/data/home/apps/s~my-app-id/module2:multi-module-1.384597049666152751/default/WEB-INF/resources/query_url.json (No such file or directory)
这是我的 GAE 模块结构:
root-ear
- default
- src/main/webapp/WEB-INF
- resources
- src/main/java (some code)
- module2
- src/main/java (code that needs the resource)
我看错文件了吗?资源文件的正确路径应该是什么?
其他详情:
我刚刚从单个模块 AppEngine 应用程序迁移 - 仅使用 classLoader.getResourceFromStream 就足以从 src/main/resources 获取我的资源,但这不再适用于耳朵结构.
我正在使用 Gradle 作为我的配置,如 appengine 示例中所建议的: https://github.com/GoogleCloudPlatform/appengine-modules-sample-java
我无法使用 ServletContext.getResources,因为我无权访问需要这些资源文件的服务中的 "servlet context"。 (虽然我可以一直传递它,如果它是绝对需要的 - 但根据文档,如果我正确知道路径,听起来 java.io.File 应该能够为我获取资源文件)
如果有人可以在我的 "build.gradle" 中建议正确的设置,将资源放在 gradle-appengine-plugin 将拾取并放置它的地方在 GAE 产品的当前路径中,这将是一个完美的解决方案!
当前目录包含"WEB-INF"和“_ah”文件夹。
以下是 WEB-INF 中的结构:
WEB-INF
- <all my files from my normal WEB-INF>
- resources <<< The folder I added under <resource-files>
- lib
- classes
- appengine-generated
我能够通过使用以下代码段列出本地文件来解决这个问题:
File curDir = new File("./WEB-INF");
File[] files = curDir.listFiles();
for (File file: files) {
logger.info(file.getName());
}