⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filebean.java

📁 该程序实现的是部门管理系统
💻 JAVA
字号:
package student.dao;
import java.io.*;
import javax.servlet.http.*;

public class FileBean {
    public String upLoadFile(HttpServletRequest request,String uploadDir,String id,String fileType )
    {  
    	String fileName=null;
    	try{  //客户的session的id
    	    String tempFileName=(String)request.getSession().getId();    	   
            File f1=new File(uploadDir,tempFileName);
            FileOutputStream o=new FileOutputStream(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();
            RandomAccessFile random=new RandomAccessFile(f1,"r");            
            String secondLine=null;
            //查看http流内容
            secondLine=random.readLine();
            while(secondLine!=null) {             	
            	System.out.println(secondLine);
            	secondLine=random.readLine();
            }
            int second=1;   //读出f1的第2行,析取出上传文件的名字:
            random.seek(0); 
            while(second<=2) { 
            	secondLine=random.readLine();
                second++;
            }
            //获取第2行中目录符号'\'最后出现的位置
             int position=secondLine.lastIndexOf('\\');
           //客户上传的文件的名字是:
            fileName=secondLine.substring(position+1,secondLine.length()-1);
            byte  cc[]=fileName.getBytes("ISO-8859-1");
            fileName=new String(cc);
            //System.out.println("fileName1="+fileName);
            //用学号和文件类型号构建唯一文件名
            position=fileName.lastIndexOf('.');
            fileName=id+"_"+fileType+"."+fileName.substring(position+1,fileName.length());
            //System.out.println("fileName2="+fileName);
            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(uploadDir,fileName); 
            RandomAccessFile random2=new RandomAccessFile(f2,"rw"); 
             //确定出文件f1中包含客户上传的文件的内容的最后位置,即倒数第6行。
            random.seek(random.length());
            long endPosition=random.getFilePointer();
            long mark=endPosition;
            int j=1;
            while((mark>=0)&&(j<=2)){   
            	mark--;
            	random.seek(mark);
            	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();
          random.close();          
          f1.delete(); //删除临时文件
       }catch(Exception exp) {   
    	   fileName=null;
       }
       return fileName;
   }
 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -