📄
字号:
<HTML>
<BODY>
<%! //声明一个需同步处理的文件:
File f=null;
String use="yes";
%>
<%--获取客户提交的小说的名字--%>
<% String name=(String)session.getAttribute("name");
byte c[]=name.getBytes("ISO-8859-1");
name=new String(c);
//获取客户续写的内容:
String content=(String)request.getParameter("messages");
if(content==null)
{content=" ";
}
%>
<%File storyFileDir=new File("F:/2000","story");
storyFileDir.mkdir();
f=new File(storyFileDir,name);
//把对文件的操作放入一个同步块中,并通知
//其它用户该文件正在被操作中:
if(use.startsWith("yes"))
{ synchronized(f)
{ use="beisusing";
try{
RandomAccessFile file=new RandomAccessFile(f,"rw");
file.seek(file.length()); //定位到文件的末尾。
file.writeUTF(content);
file.close();
use="yes";
out.print("<BR>"+"内容已经写入");
}
catch(IOException e){}
}
}
//如果该小说正在被续写,就通知客户等待:
else
{out.print("该小说正在被续写,请等待");
}
%>
</BODY>
</HTML>
例子13(效果如图4.18、4.19、4.20所示)
Example4_13.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<HTML>
<BODY>
<P>选择要上传的文件:<BR>
<FORM action="accept.jsp" method="post" ENCTYPE="multipart/form-data">
<INPUT type=FILE name="boy" size="38">
<BR>
<INPUT type="submit" name ="g" value="提交">
</BODY>
</HTML>
accept.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<HTML>
<BODY>
<%try{ InputStream in=request.getInputStream();
File f=new File("F:/2000","B.txt");
FileOutputStream o=new FileOutputStream(f);
byte b[]=new byte[1000];
int n;
while((n=in.read(b))!=-1)
{o.write(b,0,n);
}
o.close();
in.close();
}
catch(IOException ee){}
out.print("文件已上传");
%>
</BODY>
</HTML>
例子14(效果如图4.21、4.22、4.23所示)
Example4_14.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<HTML>
<BODY>
<% String str=response.encodeURL("acceptFile.jsp");
%>
<P>选择要上传的文件:<BR>
<FORM action="<%=str %>" method="post" ENCTYPE="multipart/form-data">
<INPUT type=FILE name="boy" size="45">
<BR> <INPUT type="submit" name ="g" value="提交">
</FORM>
</BODY>
</HTML>
acceptFile.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<HTML>
<BODY>
<%try{ //用客户的session的id建立一个临时文件:
String tempFileName=(String)session.getId();
//建立临时文件f1:
File f1=new File("D:/Tomcat/jakarta-tomcat-4.0/webapps/examples/",tempFileName);
FileOutputStream o=new FileOutputStream(f1);
//将客户上传的全部信息存入f1:
InputStream in=request.getInputStream();
byte b[]=new byte[10000];
int n;
while( (n=in.read(b))!=-1)
{o.write(b,0,n);
}
o.close();in.close();
//读取临时文件f1,从中获取上传文件的名字,和上传的文件的内容:
RandomAccessFile random=new RandomAccessFile(f1,"r");
//读出f1的第2行,析取出上传文件的名字:
int second=1;
String secondLine=null;
while(second<=2)
{secondLine=random.readLine();
second++;
}
//获取第2行中目录符号'\'最后出现的位置
int position=secondLine.lastIndexOf('\\');
//客户上传的文件的名字是:
String fileName=secondLine.substring(position+1,secondLine.length()-1);
random.seek(0); //再定位到文件f1的开头。
//获取第4行回车符号的位置:
long forthEndPosition=0;
int forth=1;
while((n=random.readByte())!=-1&&(forth<=4))
{ if(n=='\n')
{ forthEndPosition=random.getFilePointer();
forth++;
}
}
//根据客户上传文件的名字,将该文件存入磁盘:
File f2=new File("D:/Tomcat/jakarta-tomcat-4.0/webapps/examples/",fileName);
session.setAttribute("Name",fileName);//供showImage.jsp页面使用。
RandomAccessFile random2=new RandomAccessFile(f2,"rw");
//确定出文件f1中包含客户上传的文件的内容的最后位置,即倒数第6行。
random.seek(random.length());
long endPosition=random.getFilePointer();
long mark=endPosition;
int j=1;
while((mark>=0)&&(j<=6))
{ mark--;
random.seek(mark);
n=random.readByte();
if(n=='\n')
{ endPosition=random.getFilePointer();
j++;
}
}
//将random流指向文件f1的第4行结束的位置:
random.seek(forthEndPosition);
long startPoint=random.getFilePointer();
//从f1读出客户上传的文件存入f2(读取从第4行结束位置和倒数第6行之间的内容)。
while(startPoint<endPosition-1)
{ n=random.readByte();
random2.write(n);
startPoint=random.getFilePointer();
}
random2.close();random.close();
f1.delete(); //删除临时文件
}
catch(IOException ee){}
out.print("文件已上传");
%>
<P> 查看上传的图象效果
<%String str=response.encodeURL("showImage.jsp");
%>
<FORM action="<%=str%>">
<Input type=submit value="查看">
</FOrm >
</BODY>
</HTML>
showImage.jsp:
<HTML>
<BODY>
<% String name=(String)session.getAttribute("Name");
if(name==null)
{name="";
}
out.print("<image src=http://192.168.1.100:8080/examples/"+name);
%>
</BODY>
</HTML>
例子15(效果如图4.24、4.25所示)
Example4_15.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<HTML>
<BODY>
<P>点击超链接下载Zip文档book.Zip
<BR> <A href="loadFile.jsp">下载book.zip
</Body>
</HTML>
loadFile.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.io.*" %>
<HTML>
<BODY>
<% //获得响应客户的输出流:
OutputStream o=response.getOutputStream();
//输出文件用的字节数组,每次发送500个字节到输出流:
byte b[]=new byte[500];
//下载的文件:
File fileLoad=new File("f:/2000","book.zip");
//客户使用保存文件的对话框:
response.setHeader("Content-disposition","attachment;filename="+"book.zip");
//通知客户文件的MIME类型:
response.setContentType("application/x-tar");
//通知客户文件的长度:
long fileLength=fileLoad.length();
String length=String.valueOf(fileLength);
response.setHeader("Content_Length",length);
//读取文件book.zip,并发送给客户下载:
FileInputStream in=new FileInputStream(fileLoad);
int n=0;
while((n=in.read(b))!=-1)
{ o.write(b,0,n);
}
%>
</BODY>
</HTML>
例子16(效果如图4.26所示)
readFileByLine.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<%! //对字符串进行回压流处理的方法:
public String getString(String content)
{ try{ StringReader in=new StringReader(content) ;//指向字符串的字符流。
PushbackReader push=new PushbackReader(in); //回压流
StringBuffer stringbuffer=new StringBuffer(); //缓冲字符串对象。
int c;
char b[]=new char[1];
while ( (c=push.read(b,0,1))!=-1)//读取1个字符放入字符数组b。
{ String s=new String(b);
if(s.equals("<")) //回压的条件
{ push.unread('&');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
push.unread('L');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
push.unread('T');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
}
else if(s.equals(">")) //回压的条件
{ push.unread('&');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
push.unread('G');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
push.unread('T');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
}
else if(s.equals("\n"))
{ stringbuffer.append("<BR>");
}
else
{ stringbuffer.append(s);
}
}
push.close();
in.close();
return new String(stringbuffer); //返回处理后的字符串。
}
catch(IOException e)
{return content=new String("不能读取内容");
}
}
%>
<% String s=request.getParameter("g"); //获取客户提交的信息(是否重新读取文件)
if(s==null)
{s=""; }
byte b[]=s.getBytes("ISO-8859-1");
s=new String(b);
File f=null;
FileReader in=null;
BufferedReader buffer=null;
if(session.isNew()) //当第一次请求该页面时,建立和文件的输入流连接。
{ f=new File("f:/javabook","JSP教程.txt");
in=new FileReader(f);
buffer=new BufferedReader(in);
session.setAttribute("file",f); session.setAttribute("in",in);
session.setAttribute("buffer",buffer);
}
if(s.equals("重新读取文件")) //当请求重新读取文件时,建立和文件的输入流连接。
{ f=new File("f:/javabook","JSP教程.txt");
in=new FileReader(f);
buffer=new BufferedReader(in);
session.setAttribute("file",f); session.setAttribute("in",in);
session.setAttribute("buffer",buffer);
}
//将上述对象保存到用户的session 对象中:
try{ String str=null; int i=1;
f=(File)session.getAttribute("file");
in=(FileReader)session.getAttribute("in");
buffer=(BufferedReader)session.getAttribute("buffer");
while( ((str=buffer.readLine())!=null)&&(i<=5))
{ //为了能显示原始的HTML文件或JSP文件需使用回压流技术。
str=getString(str);
out.print("<BR>"+str);
i++;
}
}
catch(IOException e)
{ out.print("文件不存在,或读取出现问题");
}
%>
<%String code=response.encodeURL("readFileByLine.jsp");
%>
<HTML>
<BODY>
<P><BR>点击按钮读取下5行:
<FORM action="<%=code%>" method="post" name=form>
<Input type=submit value="读取文件的下5行">
</FORM>
<P><BR>点击按钮读重新读取文件:
<FORM action="" method="post" name=form>
<Input type=submit name="g" value="重新读取文件">
</FORM>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -