上传文件代码解读
<%@ 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)这些临界条件说明了什么啊?
问题比较菜一点,希望大家耐心给解答一下,感谢啊!