【发布时间】:2012-02-19 16:34:59
【问题描述】:
我想在运行时修改 Portlet 的属性文件。 Portlet 部署在 Tomcat 7.0.23 中,属性文件位于“/WEB-INF/classes/content”中,我可以通过下面显示的代码访问它。实际上,代码执行时没有任何异常,只是新添加的Property并没有保存到properties文件中。
String fileName = "MyProps.properties";
String relativePath = "/WEB-INF/classes/content/";
String fullPath = "c:/tomcat-7.0.23/webapps/my-portlet/WEB-INF/classes/content/";
try {
String path = relativePath + fileName;
InputStream in = getPortletContext().getResourceAsStream(path);
Properties props = new Properties();
props.load(in);
props.setProperty("test", "test");
File file = new File(fullPath + fileName));
FileOutputStream out = new FileOutputStream(file);
props.store(out, "");
} catch(Exception ex) { // error handling here}
添加新属性后,我可以验证
props.list(System.out);
确实添加了。 context.xml 文件包含以下条目:
antiJARLocking="true"
antiResourceLocking="true"
这是在正在运行的 Tomcat 实例中添加/更改属性的正确方法还是我应该采取不同的方法?如果是后者,如何做到最好?
非常感谢您的回答!
【问题讨论】:
-
尝试用
getPortletContext().getRealPath(relativePath+fileName)替换FileOutputStream out = new FileOutputStream(file);中的file;