如何从Web-Inf目录加载文件/属性?
题
似乎在我的Tapestry应用程序中,我无法从Web-Inf目录或类路径加载INI文件和属性文件。
我尝试了几种不同的方法,应该加载我的文件,但是它们的工作。
- Ex
realm.setResourcePath("/WEB-INF/auth.properties");
- Ex
realm.setResourcePath("classpath:wip/pages/auth.properties");
我需要加载属性/ ini文件,以便使用基于Shiro的Tapestry-Security模块。
感谢您的帮助!
- Ex
解决方案
类路径的根部是去的方式。 将您的文件放在 SRC / MAIN / REMUMATION / AUTH.PROPERTIES 中,然后设置您的 Resource路径使用 realm.setresourcepath(“classpath:auth.properties”);
检查扩展程序realmm 和rapestry-security testapp,以了解一个例子
其他提示
try ServletContext.getResourceAsStream("/WEB-INF/auth.properties")
或ServletContext.getResourceAsStream("WEB-INF/auth.properties")
servletcontext必须从servlet,servletlistener等使用。
尝试
Properties props = new Properties();
props.load(new FileInputStream(new File(req.getServletContext().getRealPath("/WEB-INF/fileName.properties"))));
System.out.println(props);
. 我发现最简单的方法是
-
将文件放在src / main / companess / config.properties中。这将在/web-inf/classes/config.properties中放入/web-inf/classes/config。通过maven汇编到WAR
-
从servlet中读取文件的文件
inputstreamInputStream= getClass()。getClassLoader()。getresourceastream(“config.properties”);
https://crunchify.com/java-properties-file-how-to-read-config-properties-values-in-java/