上传文件代码解读

happydaisy1985 2009-08-28 11:26:11
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.net.*" %>
<%@ page import="java.io.*" %>
<html>
<head>
<title>
receieve
</title>
</head>
<body bgcolor="#ffffff">
<br>
<%
String tempFile = new String("mytempfile");
File temp = new File("D:"+File.separator, tempFile);
FileOutputStream o = new FileOutputStream(temp);
InputStream fileSource = null;
fileSource = request.getInputStream();

byte[] b = new byte[8192];
int n;

while((n = fileSource.read(b)) != -1) {
o.write(b, 0, n);
o.flush();
}
fileSource.close();
o.close();


RandomAccessFile rf = new RandomAccessFile(temp, "r");
rf.readLine();
String FilePath = rf.readLine();
int position = FilePath.lastIndexOf('\\');
String filename = new String(FilePath.substring(position+1,FilePath.length()-1).getBytes("ISO-8859-1"),

"UTF-8");
rf.seek(0);
long forthEnterPosition = 0;
int forth = 1;
while((n = rf.readByte()) != -1 && (forth <= 4)) {
if(n == 10) {
forthEnterPosition = rf.getFilePointer();
forth++;
}
}

File FileUploadDir = new File("D:"+File.separator, "upload");
FileUploadDir.mkdir();

File savefile = new File("D:"+File.separator+"upload", filename);

RandomAccessFile rf2 = new RandomAccessFile(savefile, "rw");


rf.seek(rf.length());
long endPosition = rf.getFilePointer();

int j = 1;
while((endPosition>=0) && (j<=2)) {
endPosition--;
rf.seek(endPosition);
if(rf.readByte() == 10)
j++;
}

rf.seek(forthEnterPosition);
long startPoint = rf.getFilePointer();
while(startPoint + 8192L <= endPosition-1) {
n = rf.read(b);
startPoint = rf.getFilePointer();
rf2.write(b, 0, n);
}

while(startPoint < endPosition-1) {
rf2.write(rf.readByte());
startPoint = rf.getFilePointer();
}


rf2.close();
rf.close();
temp.delete();
out.println("file: " + filename + " succeed to upload!");
%>
</html>
两部分红色代码我不太明白,为什么要定义forth和j,if(n == 10);if(rf.readByte() == 10)这些临界条件说明了什么啊?
问题比较菜一点,希望大家耐心给解答一下,感谢啊!
...全文
64 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
PHPython 2009-08-31
  • 打赏
  • 举报
回复
我java菜鸟一只,你为啥不用apache commons 里面那个fileupload呢
happydaisy1985 2009-08-31
  • 打赏
  • 举报
回复
没有人明白吗?还是因为大家没有耐心帮我看看啊?
happydaisy1985 2009-08-28
  • 打赏
  • 举报
回复
再顶顶,高手们要有耐心,你一定能看明白的
happydaisy1985 2009-08-28
  • 打赏
  • 举报
回复
顶顶,大家帮忙给看看吧!

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧