第五章jsp中的文件操作实验报告

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验五:使用文件字符流加密文件
一,相关知识点
FileInputStream流以字节为单位顺序地读取文件,只要不关闭流,每次调用read方法就顺序地读取源中其余的内容,知道源的末尾或流被关闭。

FileOutStream流以字节为单位顺序地写文件,只要不关闭流,每次调用writer方法就顺序地向输出流写入内容。

二,实验目的
本实验的目的是让读者掌握使用文件输入、输出字节流读写文件。

三,实验要求
编写4个JSP页面giveContent.jsp,writeContent.jsp,lookContent.jsp,readContent.jsp以及两个Tag文件Write.tag和Read.tag。

(1)giveContent.jsp的具体要求
giveContent.jsp页面提供一个表单,要求该表单提供一个text文本输入框,select下拉列表和一个TextArea文本区,用户可以在text输入框输入文件的名字,在select下拉列表选择一个目录(下拉列表的选项必须是Tomcat服务区所驻留计算机上的目录),通过TextArea输入多行文本。

单击表单的提交键将text 中输入的文件名字,select下拉列表中选中的目录以及TextArea文本区中的内容提交给writeContent.jsp页面。

(2)writeContent.jsp的具体要求
writeContent.jsp页面首先获得giveContent.jsp页面提交的文件所在目录,名字以及TextArea文本区中的内容,然后使用Tag标记调用Tag文件Write.tag,并将文件所在目录,名字以及TextArea文本区中的内容传递给Write.tag。

(3)lookContent.jsp的具体要求
lookContent.jsp页面提够一个表单,该表单提供两个text文本输入框,用户可以向这两个text文本输入框输入目录和文件内容。

单击表单的提交键将text中输入的文件目录以及文件名字提交给readContent页面。

(4)readContent.jsp的具体要求
readContent.jsp页面首先获得lookContent.jsp页面提交的文件目录、名字,然后使用Tag标记调用Tag 文件read.jsp,并将文件所在目录、名字传递给Read.tag。

(5)Write.tag的具体要求
Write.tag文件使用attribute指令获得writeContent.jsp页面传递过来的文件目录和文件名字,然后使用文件字节输入流读取文件,并负责显示所读取的内容。

四,实验代码
JSP页面代码:
inputContent.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="file" %>
<HTML><BODY bgcolor=yellow>
<Font size=3>
<FORM action="write.jsp" Method="post">
输入文件的内容:
<br>
<TextArea name="ok" Rows="8" Cols="26"></TextArea>
<br><Input type=submit value="加密内容写入到文件">
</FORM>
<A href="read.jsp">读取文件</A>
</FONT>
</BODY></HTML>
write.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="file" %>
<HTML><BODY bgcolor=cyan>
<Font size=3>
<% String str=request.getParameter("ok");
if(str.length()>0){
byte bb[]=str.getBytes("iso-8859-1");
str=new String(bb);
%> <file:SecretWrite content="<%=str%>"/>
<% out.println("<br>"+message);
}
%>
<A href="read.jsp">读取文件</A>
</FONT>
</BODY></HTML>
read.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="file" %>
<HTML><BODY bgcolor=cyan>
<Font size=2>
<FORM action="" method=post name=form>
读取文件:<INPUT type="radio" name="R" value="secret">读取加密的文件<INPUT type="radio" name="R" value="unsecret">读取解密的文件
<INPUT TYPE="submit" value="提交" name="submit">
</FORM>
<% String condition=request.getParameter("R");
if(condition!=null){
%> <file:SecretRead method="<%=condition%>"/>
<TextArea rows=6 cols=20><%=content%><%--content是Tag文件返回的对象--%>
</TextArea>
<%}
%>
<br><A href="inputContent.jsp">返回inputContent.jsp页面</A>
</BODY></HTML>
Tag文件代码:
SecretWrite.tag
<%@ variable name-given="message" scope="AT_END" %> <%@ tag pageEncoding="GB2312" %>
<%@ tag import="java.io.*" %>
<%@ attribute name="content" required="true" %>
<% File dir=new File("C:/","Students");
dir.mkdir();
File f=new File(dir,"save.txt");
try{FileWriter outfile=new FileWriter(f);
BufferedWriter bufferout=new BufferedWriter(outfile);
char a[]=content.toCharArray();
for(int i=0;i<a.length;i++)
a[i]=(char)(a[i]^12);
content=new String(a);
bufferout.write(content);
bufferout.close();
outfile.close();
jspContext.setAttribute("message","文件加密成功");
}
catch(IOException e){
jspContext.setAttribute("message","文件加密失败"); }
%>
SecretRead.tag
<%@ tag pageEncoding="GB2312" %>
<%@ tag import="java.io.*" %>
<%@ attribute name="method" required="true" %>
<%@ variable name-given="content" scope="A T_END" %> <% File dir=new File("C:/","Students");
File f=new File(dir,"save.txt");
StringBuffer mess=new StringBuffer();
String str;
try{
FileReader in=new FileReader(f);
BufferedReader bufferin=new BufferedReader(in);
String temp;
while((temp=bufferin.readLine())!=null)
mess.append(temp);
bufferin.close();
in.close();
str=new String(mess);
if(method.equals("secret"))
jspContext.setAttribute("content",str);
else if(method.equals("unsecret")){
char a[]=str.toCharArray();
for(int i=0;i<a.length;i++)
a[i]=(char)(a[i]^12);
str=new String(a);
jspContext.setAttribute("content",str);
}
else
jspContext.setAttribute("content","");
}
catch(IOException e){
jspContext.setAttribute("content","");
}
%>五,实验结果及分析
试验总结:
计算机底层的文件都是二进制文件,本文采用字节流和字符流的区别实现文本的简单加密。

FileReader和FileWriter用于读写字符流,。

相关文档
最新文档