在jsp中进行文件的读写操作
来源:互联网 发布:前10月经济数据 编辑:程序博客网 时间:2024/05/14 23:50
1.实验目的
本实验的目的是掌握怎样在JSP中进行文件的读写操作。
2.实验内容
编写三个JSP页面giveContent.jsp,writeContent.jsp、readContent.jsp以及两个Tag文件WriteTag.tag和ReadTag.jsp。
1.giveContent.jsp的具体要求
giveConten.jsp页面提供一个表单,要求该表单提供一个text文本输入框、select下拉列表和一个TextArea文本区,用户可以在text输入框输入文件的名字、在select下拉列表选择一个目录(下拉列表的选项必须是Tomcat服务器所驻留计算机上的目录)、通过TextArea输入多行文本。单击表单的提交键将text中输入的文件名字、select下拉列表中选中的目录以及TextArea文本区中的内容提交给writeContent.jsp页面。
2.writeContent.jsp的具体要求
writeContent.jsp页面首先获得giveConten.jsp页面提交的文件所在目录、名字以及TextArea文本区中的内容,然后使用Tag标记调用Tag文件WriteTag.tag,并将文件所在目录、名字以及TextArea文本区中的内容传递给WriteTag.tag。
3.lookContent.jsp的具体要求
lookContent.jsp页面提供一个表单,该表单提供两个text文本输入框,用户可以这两个text文本输入框输入目录和文件名字。单击表单的提交键将text中输入的文件目录以及文件名字提交给readContent页面。
4.readContent.jsp的具体要求
readContent.jsp页面首先获得lookConten.jsp页面提交的文件目录、名字,然后使用Tag标记调用Tag文件ReadTag.tag,并将文件所在目录、名字传递给ReadTag.tag。
5.WriteTag.tag的具体要求
WriteTag.tag文件使用attribute指令获得writeContent.jsp页面传递过来的文件目录、文件名字和文件内容,然后使用I/O流将文件内容写入到文件中,该文件所在目录就是writeContent.jsp页面传递过来的文件目录,名字就是writeContent.jsp页面传递过来的文件名字。
6.ReadTag.tag的具体要求
Read.tag文件使用attribute指令获得readContent.jsp页面传递过来的文件目录和文件名字,然后使用I/O流读取文件,并负责显示所读取的内容。
1.2 实现代码
1,giveContent.jsp
<%@ page contentType="text/html;charset=utf-8" %><HTML><Body bgcolor=yellow><HEAD> <jsp:include page="head2.txt" /></HEAD><FORM action="writeContent.jsp" method="post" ><br>请选择一个文件目录:<select name="directory"><option value=D:\学software\jsp\WebContent>D:\学software\jsp\WebContent<option value=D:\TomCat\apache-tomcat-8.5.20\webapps\MyJsp>D:\TomCat\apache-tomcat-8.5.20\webapps\MyJsp<option value=D:\jsp文件>D:\jsp文件</select><br><br>输入保存文件的名字:<input type="text" name="fileName"><br><br>输入文件的内容:<br><TextArea name="content" rows="7" cols="40"></TextArea><br><br><input type="submit" name="submit" value="提交"></FORM></Body></HTML>
2,writeContent.jsp
<%@ page contentType="text/html;charset=utf-8" %><%@ taglib tagdir="/WEB-INF/tags" prefix="writefile" %><HTML><Body bgcolor=cyan><% request.setCharacterEncoding("utf-8"); String Dir=request.getParameter("directory"); String FName=request.getParameter("fileName"); String Con=request.getParameter("content");%><writefile:WriteTag dir="<%=Dir%>" fname="<%=FName%>" con="<%=Con%>"/><% if(result) { out.println("文件写入成功!"); out.println("<br>"+"文件所在目录:"+Dir); out.println("<br>文件的名字:"+FName); } else { out.println("文件写入失败!"); }%></Body></HTML>
3,WriteTag.tag
<%@ tag import="java.io.*" %><%@ tag pageEncoding="utf-8" %><%@ attribute name="dir" required="true" %><%@ attribute name="fname" required="true" %><%@ attribute name="con" required="true" %><%@ variable name-given="result" variable-class="java.lang.Boolean" scope="AT_END" %><%!public void writeContent(String str,File f){ try { FileWriter outfile=new FileWriter(f); BufferedWriter bufferout=new BufferedWriter(outfile); bufferout.write(str); bufferout.close(); outfile.close(); } catch(IOException e) { }}%><% boolean flag=false; File f=new File(dir,fname); /* out.println("dir"+dir); out.println("fname"+fname); out.println("con"+con); */ /* FileOutputStream op=new FileOutputStream(f); BufferedOutputStream bufferout=new BufferedOutputStream(op); byte b[]=con.getBytes(); bufferout.write(b); bufferout.flush(); bufferout.close(); op.close(); */ if(con.length()>0) { writeContent(con,f); } flag=true; jspContext.setAttribute("result",new Boolean(flag));%>
4,lookContent.jap
<%@ page contentType="text/html;charset=utf-8" %><html><body bgcolor=yellow><HEAD> <jsp:include page="head2.txt"/></HEAD><form action="readContent.jsp" method=post > 输入文件的路径(如:d/1000): <input type="text" name="path"><br> 输入文件的名字: <input type="text" name="name"><br> <input type="submit" name="submit" value="读取"></form></body></html>
5,readContent.jsp
<%@ page contentType="text/html;charset=utf-8" %><%@ taglib tagdir="/WEB-INF/tags" prefix="read" %><html><body bgcolor=cyan><% request.setCharacterEncoding("utf-8"); String filePath=request.getParameter("path"); String fileName=request.getParameter("name");%> <read:ReadTag fdir="<%=filePath%>" fname="<%=fileName%>"/></body></html>
6,ReadTag.tag
<%@ tag import="java.io.*" %><%@ tag pageEncoding="GB2312" %><%@ attribute name="fdir" required="true" %><%@ attribute name="fname" required="true" %><%! public String readContent(File f) { StringBuffer str=new StringBuffer(); try { FileReader in=new FileReader(f); BufferedReader bufferin=new BufferedReader(in); String temp; while((temp=bufferin.readLine())!=null) { str.append(temp); str.append("<br>"); } bufferin.close(); in.close(); } catch(IOException e) { } return new String(str); }%><% File f=new File(fdir,fname); String str=readContent(f); out.println(f.getName()+"的内容:<br>"); out.print(str);%>
运行结果:
若出现中文乱码:
在每个jsp页面中的request.getParemeter(“String”);前加入request.setCharacterEncoding(“utf-8”);即可
- 在jsp中进行文件的读写操作
- Linux内核中进行文件的读写操作
- 在javascript中进行cookie的读写操作
- C++中对文件进行读写操作
- C++中对文件进行读写操作
- C++中对文件进行读写操作
- C++中对文件进行读写操作
- C++中对文件进行读写操作
- C++中对文件进行读写操作
- C++中对文件进行读写操作
- C++中对文件进行读写操作
- C++中对文件进行读写操作
- C++中对文件进行读写操作
- C++中对文件进行读写操作
- C++中对文件进行读写操作
- 在SQL 脚本中进行 文件的读写
- 在TXT中进行文件读写
- 在TXT中进行文件读写
- 谈谈码率、帧率、分辨率和清晰度
- mybatis字符串匹配数字
- CODE FESTIVAL 2017 qual C
- 台大-林轩田老师-机器学习基石学习笔记2
- android 判断某个字符串包含某个字符串的个数
- 在jsp中进行文件的读写操作
- 熊猫学猿--第六竹(函数)
- 普元 EOS Platform 7.6 Oracle数据库,定时任务偶尔出现不执行问题
- Java面试题集锦
- XRecyclerView 上拉加载 下拉刷新
- window下编程的编码小坑
- Gluon新机器学习库,学习库中的富二代丨又拖了后腿,9亿4G用户平均月流量2007M【软件网每日新闻播报│第10-23期】
- 企业服务云生态居然长这样!
- CentOS7虚拟机安装、配置ZFS