2015-03-02 42 views
0

我有一个servlet,我获取所有表单值并将其存储在变量中,在该servlet本身中,我试图更新我的属性文件。但属性文件没有得到更新。任何人都可以告诉我如何访问我的表单值并更新我的属性文件。使用表单值更新属性文件不起作用

的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"); 
    FileInputStream in = new FileInputStream("server_url.properties"); 
    Properties props = new Properties(); 
    props.load(in); 
    FileOutputStream outputStream = new FileOutputStream("server_url.properties"); 
    props.setProperty("DemoApps_Links", link); 
    props.setProperty("DemoApps_DataBase", database); 
    props.store(outputStream , null); 
    outputStream .close(); 
System.out.println(link); 
System.out.println(database); 
} 
+0

你在Servlet中正确地获取参数吗? – Ved 2015-03-02 12:29:08

+0

在关闭它之前尝试刷新流:outputStream.flush(); – flayn 2015-03-02 13:06:39

回答

0

关闭输入流,然后再更新属性。

**in.close();** 
    Properties props = new Properties(); 
    props.load(in); 
    FileOutputStream outputStream = new FileOutputStream("server_url.properties"); 
    props.setProperty("DemoApps_Links", link); 
    props.setProperty("DemoApps_DataBase", database); 

假设您正在获取链接和数据库参数。

+0

我得到这个异常'java.io.IOException:流关闭' – V02169194 2015-03-03 04:16:18

+0

把它放在'props.load(in);' – Ved 2015-03-03 12:30:17