上传文件程序出现EOFException错误.

newfireflying newfireflying 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
codeToString(String s) 函数用来处理中文字符的.

<%

String tempFileName = new String( "tempFileName1");
File tempFile1 = new File("D:/",tempFileName);
FileOutputStream outputFile1 = new FileOutputStream( tempFile1 );
InputStream fileSource = request.getInputStream();
byte[] b = new byte[100000];
int n;
while( ( n=fileSource.read(b) )!= -1 )
{
outputFile1.write(b,0,n);
}
outputFile1.close();
fileSource.close();

RandomAccessFile randomFile1 = new RandomAccessFile( tempFile1,"r" );
randomFile1.readLine();
String filePath = randomFile1.readLine();
int position = filePath.lastIndexOf('\\');
String filename = codeToString( filePath.substring( position+1,filePath.length()-1 ) );

randomFile1.seek(0);
long forthEnterPosition=0;
int forth = 1;

while( ( ( n= randomFile1.readByte() ) !=-1 )&&( forth<=4 ) ) //出现异常.
{
if( n=='\n')
{
forthEnterPosition = randomFile1.getFilePointer();
forth++;
}
}

File fileUpLoadDir = new File("D:/","upload" );
fileUpLoadDir.mkdir();
File saveFile1 = new File("D:/upload",filename);
RandomAccessFile randomFile2 = new RandomAccessFile( saveFile1 ,"rw" );

randomFile1.seek( randomFile1.length() );
long endPosition = randomFile1.getFilePointer();
int j = 1;
while( endPosition >=0 && j<=4 )
{
endPosition--;
randomFile1.seek( endPosition );
if( randomFile1.readByte() == 10 )
j++;
}

randomFile1.seek(forthEnterPosition);
long startPoint = randomFile1.getFilePointer();
while( startPoint< endPosition-1 )
{
randomFile2.write( randomFile1.readByte() );
startPoint = randomFile1.getFilePointer();
}
randomFile2.close();
randomFile1.close();
tempFile1.delete();
out.print("文件"+filename+"已上传成功<br>");

%>

异常信息:java.io.EOFException
java.io.RandomAccessFile.readByte(RandomAccessFile.java:580)
org.apache.jsp.process_jsp._jspService(process_jsp.java:99)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

为什么会出现这个异常?读到文件尾了吗?
我不是已经判断了 n!=-1才读取文件.
...全文
评论 2
收藏 已收藏
分享
返回顶部
newfireflying
Web 开发 81,108 成员 341,721 内容