现在的位置: 首页 > 综合 > 正文

在jsp页面向文件中写入信息时,文件路径如何处理?

2013年08月14日 ⁄ 综合 ⁄ 共 6013字 ⁄ 字号 评论关闭

这两天做Web项目时需要向配置文件中写入信息,可是调试良久未果。具体情况如下:

Web项目testproject中需要向src下的一个配置文件system.properties中写入信息。我在页面中通过String realPath = application.getRealPath("/")方式可以获取当前项目发布后的根路径(即D:/ToolSoft/apache-tomcat-6.0.32/webapps/testproject/)。而那个配置文件在项目下的路径为WEB-INF/classes/system.properties。我想以new File(realPath + “WEB-INF/classes/system.properties”);的方式创建一个文件对象并向其中写入信息,可是程序执行时出错。

 

我觉得应该是那个路径和java里面的路径分隔符不一致的问题。于是,有人建议我直接在创建File对象时把路径写成固定的。可我觉得这样不好,因为项目重新发布时还要修改该路径。上网找了一下,有人说用File.separator可以解决问题,但我试了半天也没有能够解决。

无奈,最后向我一个老师求助。老师回复说【首先确定你的路径是否正确,其二,试试双斜杠,有些在处理路径的时候要这样,不认识单斜杠】。按照老师的指点又尝试一番,最后终于解决问题。

其实也比较简单,我把相关代码发上来供大家参考!

 

项目结构:
index.jsp页面源码:
Code:
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ page import="org.test.TestServlet,java.io.*"%>  
  3. <%  
  4.     String path = request.getContextPath();  
  5.     String basePath = request.getScheme() + "://"  
  6.             + request.getServerName() + ":" + request.getServerPort()  
  7.             + path + "/";  
  8. %>  
  9. <%  
  10.     //读取配置文件  
  11.     Properties props = new Properties();  
  12.     try  
  13.     {  
  14.         InputStream in = TestServlet.class.getClassLoader()  
  15.                 .getResourceAsStream("system.properties");  
  16.         props.load(in);  
  17.     } catch (IOException e)  
  18.     {  
  19.         System.out.println("配置文件读取失败!");  
  20.     }  
  21.   
  22.     //获取配置信息  
  23.     String author = props.getProperty("author");  
  24.     if (author == null)  
  25.         author = "";  
  26. %>  
  27. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  28. <html>  
  29.   <head>  
  30.     <base href="<%=basePath%>">  
  31.       
  32.     <title>My JSP 'index.jsp' starting page</title>  
  33.     <meta http-equiv="pragma" content="no-cache">  
  34.     <meta http-equiv="cache-control" content="no-cache">  
  35.     <meta http-equiv="expires" content="0">      
  36.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  37.     <meta http-equiv="description" content="This is my page">  
  38.     <!-- 
  39.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  40.     -->  
  41.   </head>  
  42.     
  43.   <body>  
  44.     <center>修改系统信息<br/>  
  45.         <form action="test" method="post">  
  46.           
  47.         <!-- 获取当前项目发布后在服务器的真实根路径 -->  
  48.             <input type="hidden" name="realPath"  
  49.                 value="<%=application.getRealPath("/")%>/>  
  50.           
  51.         <!-- 显示配置文件中的配置信息 -->  
  52.             author: <input type="text" name="author" value="<%=author%>" />  
  53.             <br/>  
  54.             <input type="submit" value="submit" />  
  55.         </form>  
  56.     </center>  
  57.     </body>  
  58. </html>  

web.xml文件内容:

Code:
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  6.     <servlet>  
  7.         <servlet-name>TestServlet</servlet-name>  
  8.         <servlet-class>org.test.TestServlet</servlet-class>  
  9.     </servlet>  
  10.   
  11.     <servlet-mapping>  
  12.         <servlet-name>TestServlet</servlet-name>  
  13.         <url-pattern>/test</url-pattern>  
  14.     </servlet-mapping>  
  15.     <welcome-file-list>  
  16.         <welcome-file>index.jsp</welcome-file>  
  17.     </welcome-file-list>  
  18. </web-app>  

TestServlet.java文件内容:

Code:
  1. package org.test;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.OutputStream;  
  7. import java.util.Properties;  
  8.   
  9. import javax.servlet.ServletException;  
  10. import javax.servlet.http.HttpServlet;  
  11. import javax.servlet.http.HttpServletRequest;  
  12. import javax.servlet.http.HttpServletResponse;  
  13.   
  14. public class TestServlet extends HttpServlet  
  15. {  
  16.     /** 
  17.      * The doGet method of the servlet. <br> 
  18.      * 
  19.      * This method is called when a form has its tag value method equals to get. 
  20.      *  
  21.      * @param request the request send by the client to the server 
  22.      * @param response the response send by the server to the client 
  23.      * @throws ServletException if an error occurred 
  24.      * @throws IOException if an error occurred 
  25.      */  
  26.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  27.             throws ServletException, IOException  
  28.     {  
  29.         doPost(request, response);  
  30.     }  
  31.   
  32.     /** 
  33.      * The doPost method of the servlet. <br> 
  34.      * 
  35.      * This method is called when a form has its tag value method equals to post. 
  36.      *  
  37.      * @param request the request send by the client to the server 
  38.      * @param response the response send by the server to the client 
  39.      * @throws ServletException if an error occurred 
  40.      * @throws IOException if an error occurred 
  41.      */  
  42.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  43.             throws ServletException, IOException  
  44.     {  
  45. //      Sets the character encoding (MIME charset) of the response being sent to the client  
  46.         response.setCharacterEncoding("UTF-8");  
  47.           
  48.         String realPath = request.getParameter("realPath");  
  49.         String author = request.getParameter("author");  
  50.           
  51. //      我原来两种错误的写法:  
  52. //      String filePath = realPath + "WEB-INF/classes/systeminfo.properties";  
  53. //      String filePath = realPath.replaceAll("//", "/") + "WEB-INF/classes/systeminfo.properties";  
  54. //      下面是正确的写法:  
  55.         String filePath = realPath + "WEB-INF//classes//system.properties";  
  56.           
  57.         Properties prop = new Properties();  
  58.         try  
  59.         {  
  60.             File f = new File(filePath);  
  61.             OutputStream fos = new FileOutputStream(f);  
  62.               
  63.             prop.setProperty("author", author);  
  64.               
  65. //          将此 Properties 表中的属性列表(键和元素对)写入输出流。  
  66.             prop.store(fos, "Update 'author' value");  
  67.             response.getWriter().write("系统信息更新成功!");  
  68.         } catch (IOException e)  
  69.         {  
  70.             System.err.println("Visit " + filePath + " for updating author value error");  
  71.             e.printStackTrace();  
  72.         }  
  73.     }  
  74. }  

system.properties文件内容:

Code:
  1. #Update 'author' value  
  2. #Thu May 26 13:00:59 CST 2011  
  3. author=Tom  

因为是一个测试例子,所以项目中的流程十分简单,我就不多废话了!

最后附上该测试例子源码(可直接导入MyEclipse 8.5):

 http://download.csdn.net/source/3314443

抱歉!评论已关闭.