我的项目下的资源文件夹中有一个属性文件。我能够加载文件,但当我尝试写入它时,它不会更新。我确信这个问题已被问过好几次了。如果有人为我提供解决方案,我将非常感激。
的Servlet
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String name = request.getParameter("appName");
String link = request.getParameter("appLink");
String database = request.getParameter("appDB");
String webServices = request.getParameter("appWebService");
System.out.println(name);
Properties prop = new Properties();
String propFileName = "server_url.properties";
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(propFileName);
prop.load(inputStream);
System.out.println(propFileName);
if (name.equalsIgnoreCase(prop.getProperty("DemoApps_name"))) {
prop.setProperty("DemoApps_DataBase", database);
prop.setProperty("DemoApps_Links", link);
prop.setProperty("DemoApps_WebServices", webServices);
System.out.println(prop.getProperty("DemoApps_DataBase"));
}
prop.store(new FileWriter(propFileName),"Update App details" ); System.out.println(propFileName +" "+"written successfully");
response.sendRedirect("updateappstatus.jsp");
}
答案 0 :(得分:1)
首先,您没有写入与原始文件相同的位置。 在写入当前工作目录时,可以从类路径中读取原始文件。
其次,您不应该写入应用程序的类路径。如果你在你的网络应用程序中发布一个属性文件,你不能修改它(我不知道Servlet规范到那个细节,但有一个合同禁止你这样做,或者甚至可能容器实现者保持
表示您的Web应用程序在打包的jar / war文件中的类,因此类和资源是只读的。您可以做的是发送一个默认属性文件,您将其视为后备,并将您的可写属性文件保存在容器外的位置,如此问题的答案中所述:Where/how to store persistent data with tomcat?
答案 1 :(得分:1)
我遇到了同样的问题。 Web项目将具有2个不同的运行位置。在eclipse上运行它时,项目将被加载到Eclipse workspace
目录。因此,如果您想从该位置访问您的属性文件,您可以执行以下操作。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String currentPath = getServletContext().getRealPath("/") + "/WEB-INF/classes/com/server_url.properties";
String name = request.getParameter("name");
System.out.println(name);
try {
System.out.println(getPropertyValue("name",currentPath));
setPropertyValue("name", name,currentPath);
System.out.println(getPropertyValue("name",currentPath));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void setPropertyValue(String key, String value,String path) throws Exception {
java.io.InputStream inputStream = null;
FileOutputStream outputProperty = null;
try {
Properties projectProperties = new Properties();
inputStream = new FileInputStream( path);
projectProperties.load(inputStream);
outputProperty = new FileOutputStream( path);
projectProperties.setProperty(key, value);
projectProperties.store(outputProperty, null);
} catch (Exception e) {
throw e;
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputProperty != null) {
outputProperty.close();
}
}
}
public static String getPropertyValue(String key,String path) throws Exception {
java.io.InputStream inputStream = null;
try {
Properties projectProperties = new Properties();
inputStream = new FileInputStream(path);
projectProperties.load(inputStream);
return projectProperties.getProperty(key);
} catch (Exception e) {
throw e;
} finally {
if (inputStream != null) {
inputStream.close();
}
}
}
这些代码将用于获取或设置属性值。另一种最好的方法是在Web应用程序之外使用属性文件。