如何读取应用引擎应用程序中的属性文件?

M.S*_*idu 1 google-app-engine file

我正在从我的 google appengine 应用程序的文件夹 (/war/config/client.properties) 中读取属性文件。它在我的本地服务器上工作正常,但在生产模式下无法正常工作,并且抛出异常 java.security.AccessControlException: access denied (java.io.FilePermission)。

你能告诉我如何使它在生产模式下工作吗?

use*_*245 5

如果你把它放在 WEB-INF/classes 中,你可以像这样加载它:

InputStream is =  this.getClass().getClassLoader().getResourceAsStream("/client.properties");
Run Code Online (Sandbox Code Playgroud)

同样你可以做

InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/client.properties");
Run Code Online (Sandbox Code Playgroud)

然后:

Properties props = new Properties();
props.load(is);
String whatever = props.getProperty("whatever_key");
Run Code Online (Sandbox Code Playgroud)

根据文档https://developers.google.com/appengine/docs/java/?hl=en-US#The_Sandbox

默认情况下,战争中的任何内容都应该起作用:

确保您没有排除 appengine-web.xml 中的任何内容):

  <resource-files>
        <exclude path="**.properties"/> ...make sure you haven't done this
  </resource-files>
Run Code Online (Sandbox Code Playgroud)

默认情况下,战争中的任何内容都应该有效,因此请尝试阅读/config/client.properties

java.io.FileReader 肯定会。

或者

InputStream is = this.getServletContext().getResourceAsStream("/config/client.properties");
Run Code Online (Sandbox Code Playgroud)