gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:12:28 27 4
gpt4 key购买 nike

我创建了两个文件1)index.html2)播放器.jsp我正在使用 Tomcat 服务器。在index.html中,我在表单中创建了一些复选框并分配了值...单击提交后,我将这些值转发到player.jsp...在player.jsp中,我动态生成了一个名为“generate.xml”的xml文件。该 XML 文件根据用户请求而更改。对于每个新请求,XML 文件都应该被覆盖。如果在表单中选中一个复选框,则generate.xml中的结构将采用一种表单如果在表单中选中两个复选框,则generate.xml中的结构将是另一种表单。我在 jsp 页面中嵌入了一个 Flash 播放器,该播放器以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 文件..

最佳答案

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

[已编辑]

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

关于java - 如何覆盖保存在上下文目录中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/535649/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com