public void uploadFile() throws IOException {
renderNull();
//==================开始处理文件===================
//接收上传文件内容中临时文件的文件名
System.out.println(getRequest().getContentLength());
if(getRequest().getContentLength()>0){
String tempFileName = new String("tempFileName.txt");
//tempfile 对象指向临时文件
File tempFile = new File(PathKit.getWebRootPath() + File.separator+tempFileName);
System.out.println("FilePath:"+PathKit.getWebRootPath() + File.separator+tempFileName);
//outputfile 文件输出流指向这个临时文件
FileOutputStream outputStream = new FileOutputStream(tempFile);
//得到客户端提交的所有数据
InputStream fileSourcel = getRequest().getInputStream();
//将得到的客户端数据写入临时文件
byte b[] = new byte[1000];
int n ;
while ((n=fileSourcel.read(b))!=-1){
System.out.println("b:"+b);
System.out.println("n:"+n);
outputStream.write(b,0,n);
}
//关闭输出流和输入流
outputStream.close();
fileSourcel.close();
//randomFile对象指向临时文件
RandomAccessFile randomFile = new RandomAccessFile(tempFile,"r");
//读取临时文件的前三行数据
randomFile.readLine();
randomFile.readLine();
randomFile.readLine();
//读取临时文件的第四行数据,这行数据中包含了文件的路径和文件名
String filePath = randomFile.readLine();
//得到文件名
System.out.println(filePath);
int position = filePath.lastIndexOf("filename");
String filename =Tool.codeString(filePath.substring(position+10,filePath.length()-1));
System.out.println("filename"+filename);
//重新定位读取文件指针到文件头
randomFile.seek(0);
//得到第五行回车符的位置,这是上传文件数据的开始位置
long fifthEnterPosition = 0;
int fifth = 1;
while((n=randomFile.readByte())!=-1&& fifth<=5)){
System.out.println("n:"+n);
System.out.println( fifth:" fifth);
if(n==‘\n‘){
fifthEnterPosition = randomFile.getFilePointer();
fifth++;
}
System.out.println( fifthEnterPosition:" fifthEnterPosition);
}
//生成上传文件的目录
File fileupLoad = new File(PathKit.getWebRootPath() + File.separator,"upLoad");
fileupLoad.mkdir();
//saveFile 对象指向要保存的文件
File saveFile = new File(PathKit.getWebRootPath() +"\\upLoad",filename);
RandomAccessFile randomAccessFile = new RandomAccessFile(saveFile,"rw");
//找到上传文件数据的结束位置,即倒数第三行
randomFile.seek(randomFile.length());
long endPosition = randomFile.getFilePointer();
System.out.println("endPosition:"+endPosition);
int j = 1;
while((endPosition>=0)&&(j<=3)){
System.out.println("endPosition1:"+endPosition);
System.out.println("j:"+j);
endPosition--;
randomFile.seek(endPosition);
if(randomFile.readByte()==‘\n‘){
j++;
}
}
//从上传文件数据的开始位置到结束位置,把数据写入到要保存的文件中
System.out.println( fifthEnterPosition:" fifthEnterPosition);
randomFile.seek fifthEnterPosition);
long startPoint = randomFile.getFilePointer();
System.out.println("startPoint:"+startPoint);
//endPosition=randomFile.getFilePointer();
System.out.println("endPosition:"+endPosition);
while(startPoint<endPosition){
System.out.println("startPoint1:"+startPoint);
//System.out.println("randomFile.readByte():"+randomFile.readByte());
randomAccessFile.write(randomFile.readByte());
startPoint = randomFile.getFilePointer();
}
//关闭文件输入、输出
randomAccessFile.close();
randomFile.close();
tempFile.delete();
//==================处理文件结束===================
//向控制台输出文件上传成功
System.out.println("File upload success!");
checkCsv();
}else{
renderText("false");
}
}