我正在为学校编写一个在线文本编辑器。我已经能够使用自定义名称保存文件,所以这不是问题。使用我拥有的 HTML 表单,它将 TextArea 的文本提交到用户指定的文件中,然后将 url 设置为 blah.org/text.jsp?status=gen。在后面的代码中,如果变量 status == gen,我让程序写入文件,否则什么也不做。单击提交时,我无法创建文件,我认为这与我在 URL 中获取变量有关。这是我的代码:
/* Set the "user" variable to the "user" attribute assigned to the session */
String user = (String)session.getAttribute("user");
String status = request.getParameter("status");
/* Get the name of the file */
String name = request.getParameter("name");
/* Set the path of the file */
String path = "C:/userFiles/" + user + "/" + name + ".txt";
/* Get the text in a TextArea */
String value = request.getParameter("textArea");
/* If there isn't a session, tell the user to Register/Log In */
if (null == session.getAttribute("user")) {
out.println("Please Register/Log-In to continue!");
if (status == "gen") {
try {
FileOutputStream fos = new FileOutputStream(path);
PrintWriter pw = new PrintWriter(fos);
pw.println(value);
pw.close();
fos.close();
}
catch (Exception e) {
out.println("<p>Exception Caught!");
}
} else {
out.println("Error");
}
}
和形式:
<form name="form1" method="POST" action="text.jsp?status=gen">
<textarea cols="50" rows="30" name="textArea"></textarea>
<center>Name: <input type="text" name="name" value="Don't put a .ext"> <input type="submit" value="Save" class="button"></center>
other code here </form>