以下代码总提示有空异常,请高手帮分析一下!!
<%@ 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("C:\\Tomcat\\webapps\\ROOT\\adn\\files",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("C:\\Tomcat\\webapps\\ROOT\\adn\\files",fileName);
//供showImage.Jsp使用
RandomAccessFile random2=new RandomAccessFile(f2,"rw");
//确定用户文件f1中包含用户上传的文件的最后的位置,即倒数第6行
random.seek(random.length());
long endPosition=random.getFilePointer();
long =endPosition;
int j=1;
while ((>=0) && (j<=6))
{
--;
random.seek();
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(); //删除临时文件
}
catch(IOException ee){}
out.print("文件已经上传");
%>
<p>hello world!!!
</body>
</html>
<%@ 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("C:\\Tomcat\\webapps\\ROOT\\adn\\files",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("C:\\Tomcat\\webapps\\ROOT\\adn\\files",fileName);
//供showImage.Jsp使用
RandomAccessFile random2=new RandomAccessFile(f2,"rw");
//确定用户文件f1中包含用户上传的文件的最后的位置,即倒数第6行
random.seek(random.length());
long endPosition=random.getFilePointer();
long =endPosition;
int j=1;
while ((>=0) && (j<=6))
{
--;
random.seek();
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(); //删除临时文件
}
catch(IOException ee){}
out.print("文件已经上传");
%>
<p>hello world!!!
</body>
</html>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货