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

这两天做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. <%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
  2. <%@pageimport="org.test.TestServlet,java.io.*"%>
  3. <%
  4. Stringpath=request.getContextPath();
  5. StringbasePath=request.getScheme()+"://"
  6. +request.getServerName()+":"+request.getServerPort()
  7. +path+"/";
  8. %>
  9. <%
  10. //读取配置文件
  11. Propertiesprops=newProperties();
  12. try
  13. {
  14. InputStreamin=TestServlet.class.getClassLoader()
  15. .getResourceAsStream("system.properties");
  16. props.load(in);
  17. }catch(IOExceptione)
  18. {
  19. System.out.println("配置文件读取失败!");
  20. }
  21. //获取配置信息
  22. Stringauthor=props.getProperty("author");
  23. if(author==null)
  24. author="";
  25. %>
  26. <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
  27. <html>
  28. <head>
  29. <basehref="<%=basePath%>">
  30. <title>MyJSP'index.jsp'startingpage</title>
  31. <metahttp-equiv="pragma"content="no-cache">
  32. <metahttp-equiv="cache-control"content="no-cache">
  33. <metahttp-equiv="expires"content="0">
  34. <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
  35. <metahttp-equiv="description"content="Thisismypage">
  36. <!--
  37. <linkrel="stylesheet"type="text/css"href="styles.css">
  38. -->
  39. </head>
  40. <body>
  41. <center>修改系统信息<br/>
  42. <formaction="test"method="post">
  43. <!--获取当前项目发布后在服务器的真实根路径-->
  44. <inputtype="hidden"name="realPath"
  45. value="<%=application.getRealPath("/")%>"/>
  46. <!--显示配置文件中的配置信息-->
  47. author:<inputtype="text"name="author"value="<%=author%>"/>
  48. <br/>
  49. <inputtype="submit"value="submit"/>
  50. </form>
  51. </center>
  52. </body>
  53. </html>

web.xml文件内容:

Code:
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <web-appversion="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. <servlet-mapping>
  11. <servlet-name>TestServlet</servlet-name>
  12. <url-pattern>/test</url-pattern>
  13. </servlet-mapping>
  14. <welcome-file-list>
  15. <welcome-file>index.jsp</welcome-file>
  16. </welcome-file-list>
  17. </web-app>

TestServlet.java文件内容:

Code:
  1. packageorg.test;
  2. importjava.io.File;
  3. importjava.io.FileOutputStream;
  4. importjava.io.IOException;
  5. importjava.io.OutputStream;
  6. importjava.util.Properties;
  7. importjavax.servlet.ServletException;
  8. importjavax.servlet.http.HttpServlet;
  9. importjavax.servlet.http.HttpServletRequest;
  10. importjavax.servlet.http.HttpServletResponse;
  11. publicclassTestServletextendsHttpServlet
  12. {
  13. /**
  14. *ThedoGetmethodoftheservlet.<br>
  15. *
  16. *Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.
  17. *
  18. *@paramrequesttherequestsendbytheclienttotheserver
  19. *@paramresponsetheresponsesendbytheservertotheclient
  20. *@throwsServletExceptionifanerroroccurred
  21. *@throwsIOExceptionifanerroroccurred
  22. */
  23. publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
  24. throwsServletException,IOException
  25. {
  26. doPost(request,response);
  27. }
  28. /**
  29. *ThedoPostmethodoftheservlet.<br>
  30. *
  31. *Thismethodiscalledwhenaformhasitstagvaluemethodequalstopost.
  32. *
  33. *@paramrequesttherequestsendbytheclienttotheserver
  34. *@paramresponsetheresponsesendbytheservertotheclient
  35. *@throwsServletExceptionifanerroroccurred
  36. *@throwsIOExceptionifanerroroccurred
  37. */
  38. publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
  39. throwsServletException,IOException
  40. {
  41. //Setsthecharacterencoding(MIMEcharset)oftheresponsebeingsenttotheclient
  42. response.setCharacterEncoding("UTF-8");
  43. StringrealPath=request.getParameter("realPath");
  44. Stringauthor=request.getParameter("author");
  45. //我原来两种错误的写法:
  46. //StringfilePath=realPath+"WEB-INF/classes/systeminfo.properties";
  47. //StringfilePath=realPath.replaceAll("//","/")+"WEB-INF/classes/systeminfo.properties";
  48. //下面是正确的写法:
  49. StringfilePath=realPath+"WEB-INF//classes//system.properties";
  50. Propertiesprop=newProperties();
  51. try
  52. {
  53. Filef=newFile(filePath);
  54. OutputStreamfos=newFileOutputStream(f);
  55. prop.setProperty("author",author);
  56. //将此Properties表中的属性列表(键和元素对)写入输出流。
  57. prop.store(fos,"Update'author'value");
  58. response.getWriter().write("系统信息更新成功!");
  59. }catch(IOExceptione)
  60. {
  61. System.err.println("Visit"+filePath+"forupdatingauthorvalueerror");
  62. e.printStackTrace();
  63. }
  64. }
  65. }

system.properties文件内容:

Code:
  1. #Update'author'value
  2. #ThuMay2613:00:59CST2011
  3. author=Tom

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值