jsp中的文件操作(一)

创建两个文件selectContent.jsp和writeContent.jsp。首次使用selectContent.jsp中的表单输入存放路径、保存的文件名和将要写入文件的文件内容信息提交后,由writeCountent.jsp文件负责将内容写到指定的文件中,同时放到指定的路径下,实验效果如下图
在这里插入图片描述
明白你们都不想看我多说废话,那么直接上源代码了
selectContent.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <form action="writeContent.jsp">
              请输入存放路径:<br>
        <input type="text" name="path" /><br>
        输入保存文件名字:<br>
        <input type="text" name="file" /><br>
        输入文件内容:<br>
        <input type="text" value="请输入内容..." style={height:400px;} name="content"><br><br>
        <input type="submit" value="提交" name="submit">
    </form>
    
  </body>
</html>

wirteContent.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@page import="java.io.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'writeContent.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head> 
  <body>
     <%
         String Path = request.getParameter("path");
         String file_name = request.getParameter("file");
         String content = request.getParameter("content");
         //取得代表目录中所有文件的file对象数组
         File d = new File(Path);
         File list[] = d.listFiles();
         for(int i=0;i<list.length;i++){
             if(list[i].getName().equals(file_name)){
                 out.print(file_name+"文件已存在!将在源文件中添加内容");
                
             }            
         }
         
                  out.print("正在写入,请稍等...<br>");
                  FileWriter fw = new FileWriter(Path+"\\"+file_name);
                  fw.write(content);
                  fw.close();
                  out.print("写入成功<br>");
                  out.print("写入内容:"+content+"<br>");
                  out.print("文件所在目录:"+Path+"<br>");
                  out.print("文件的名字:"+file_name+"<br>");
                   
        
      %>
  </body>
</html>

运行效果:
在这里插入图片描述

提交之后:
在这里插入图片描述
如果碰巧这篇文章对你有所帮助,希望客官点个赞哟,你的点赞就是我更新的动力!
下一篇:jsp中的文件操作(二)

发布了33 篇原创文章 · 获赞 70 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_42878211/article/details/105433803
今日推荐