如何覆盖上下文目录中保存的文件

时间:2009-02-11 06:29:29

标签: java html xml jsp

我创建了两个文件 1)的index.html 2)player.jsp 我正在使用Tomcat服务器。 在index.html中,我在表单中创建了一些复选框并分配了值... 点击提交后,我将这些值转发给了player.jsp ... 在player.jsp中,我动态生成了一个名为“generate.xml”的xml文件。 此XML文件根据用户请求而更改。 对于每个新请求,应覆盖xml文件。 如果在表单中选中了一个复选框,则generate.xml中的结构将采用一种形式 如果在表单中选中了两个复选框,则generate.xml中的结构将是另一种形式。 我在jsp页面中嵌入了一个flash player,它将generate.xml作为输入并相应地播放歌曲...... player.jsp的代码是

<%@ page import="java.io.*" %>
<%@ page contentType="text/html;charset=ISO-8859-1" %> 
<%
int iLf = 10;
char cLf = (char)iLf;
String myfile = application.getRealPath("/") + "generate.xml";

File outputFile = new File(myfile);
outputFile.createNewFile();
FileWriter outfile = new FileWriter(outputFile);
outfile.write(" <?xml version='1.0' encoding='UTF-8'?> "+cLf);
outfile.write(" <playlist version='1' xmlns = 'http://xspf.org/ns/0/' > " +cLf);
outfile.write(" <title>My Band Rocks Your Socks</title> "+cLf); 
outfile.write("<trackList>"+cLf); 
%>
 <%! String[] sports; %>
 <%
    sports = request.getParameterValues("sports");

    out.println("<html><body><h1>hello</h1></body></html>");

    if (sports != null)
    { 
         for (int i = 0; i < sports.length; i++)
         { 
              // outfile.writeln (sports[i]); 
              String total=sports[i];
              String[] sa=total.split("[,]");
              // String[] sub=new String();
              outfile.write("<track>"+cLf);
              for (int j=0;j<sa.length;j++)
              {
                // outfile.writeln(sa[j]);
                // outfile.writeln("sa["+j+"]="+sa[j]);
                if( j == 0)
                {
                     outfile.write("<location>" + sa[0] +"</location>"+cLf); 
                }
                else if (j == 1)
                     {
                        outfile.write("<image>" + sa[1] +"</image>"+cLf); 
                     }
                     else if( j==2)
                          {
                            outfile.write("<title>" + sa[2] +"</title>"+cLf);
                          }

               }// end of inner for loop()       
               outfile.write("</track>"+cLf);
         //outfile.writeln();
      }// end of outer for()
    } 
    //else outfile.writeln ("<b>none<b>");

  outfile.write(" </trackList> "+cLf);
  outfile.write(" </playlist> "+cLf);
  outfile.close();

  %>
<object type="application/x-shockwave-flash" width="400" height="170"
          data="xspf_player.swf?playlist_url=generate.xml">
          <param name="movie" value="xspf_player.swf?playlist_url=generate.xml" />

</object>

我的问题是在我的本地系统中,所有这一切都正常 并且每次为每个新请求覆盖generate.xml ... 我创建了ROOT.war并将此文件上传到www.eatj.com 这里提交第一个请求时,会根据请求创建一个generate.xml文件。 对于下一个请求,此generate.xml为 NOT OVERWRITTEN 。 播放器正在获取由第一个请求生成的generate.xml文件,以用于所有新请求。 请帮助我在代码中进行任何更改,以便我可以覆盖以前生成的xml文件..

1 个答案:

答案 0 :(得分:1)

方法 getRealPath() war 下不起作用,并返回 null ,AFAIR。您必须使用相对路径才能使其正常工作。 Request.getResourceAsStream()是一个更好的选择。由于这个原因,我们不鼓励使用 getRealPath()

<强> [编辑]

Found a thread of coderanch证实了我的怀疑。