| |
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*;
public class uploadbean { servletrequest request; servletinputstream input; string objectdir="c:/upload/"; private int m_currentindex; private int max_file_size=50*1024*1024; private byte[] m_binaries; private string m_boundary; private int contentlength; private string filename="";
public uploadbean(){ super(); m_currentindex=0; } public uploadbean(servletrequest request){ this(); this.setrequest(request); } public void setrequest(servletrequest request){ if (request!=null){ this.request=request; try{ this.setinputstream(request.getinputstream()); }catch(ioexception ioe){ system.out.println("ioexception occurred in com.javacat.jsp.beans.upload.uploadbean.setrequest:"+ioe.getmessage()); } } }
public servletrequest getrequest(){ return this.request; }
public void setinputstream(servletinputstream inputstream){ this.input=inputstream; }
public servletinputstream getinputstream(){ return this.input; }
public outputstream getoutputstream(string filename) throws ioexception{ int findex=filename.lastindexof("/"); file file=new file(getobjectdir(), filename.substring(findex+1)); this.filename=filename.substring(findex+1); return new fileoutputstream(file); }
public void setobjectdir(string dir){ this.objectdir=dir; } public string getobjectdir(){ return this.objectdir; } public string getfilename(){ return this.filename; } public int upload() throws ioexception,securityexception{ if(request==null) return -2; boolean isfile; string dataheader; string filename=""; byte[] thebytes; int countfile=0; outputstream output; contentlength =request.getcontentlength();
m_binaries=new byte[contentlength]; int haveread=0; while (haveread<contentlength) { haveread+=getinputstream().read(m_binaries, haveread, contentlength-haveread); } //system.out.println("content= "+new string(m_binaries)); boolean match=false; m_boundary=new string(); for (; !match && m_currentindex<contentlength; m_currentindex++ ){ if(m_binaries[m_currentindex]==´´) match=true; else m_boundary=m_boundary+(char)m_binaries[m_currentindex]; } if (m_currentindex==1) return -1; m_currentindex++; do{ if(m_currentindex>=contentlength) break; dataheader=getdataheader(); m_currentindex=m_currentindex+2; isfile=dataheader.indexof("filename")>0; if (isfile) { filename=""; if(!getfilepath(dataheader).equals("")) filename=getfilename(getfilepath(dataheader)); if((filename==null)||(filename.equals(""))) isfile=false; } thebytes=getdatasection(); if(isfile){ if(thebytes.length>this.max_file_size) throw new securityexception("file size is too large.10m bytes is a limited"); else{ output=getoutputstream(filename); output.write(thebytes); countfile++; output.close(); } } if ((char)m_binaries[m_currentindex+1]==´-´) break; m_currentindex=m_currentindex+2; }while(true); return countfile; }
private byte[] getdatasection(){ int searchposition=m_currentindex; int keyposition=0; int boundarylen=m_boundary.length(); int start=m_currentindex; int end=m_currentindex; do{ if(searchposition>=contentlength ) break; if(m_binaries[searchposition]==(byte)m_boundary.charat(keyposition)){ if(keyposition==boundarylen-1){ end=searchposition - boundarylen - 2; break; } searchposition++; keyposition++; } else{ searchposition++; keyposition=0; } }while(true); m_currentindex=end+boundarylen+3; byte[] data=new byte[end-start+1]; for(int i=0; i<data.length; i++) data[i]=m_binaries[start+i]; return data; }
private string getdataheader(){ int start =m_currentindex; int end=0; int len=0; boolean match=false; while(!match){ if(m_binaries[m_currentindex]==´´ && m_binaries[m_currentindex+2]==´´){ match=true; end=m_currentindex-1; m_currentindex=m_currentindex+2; } else{ m_currentindex++; } } return new string(m_binaries,start, (end- start )+1); }
private string getfilename(string filepathname){ int pos=-1; pos=filepathname.lastindexof(´/´)+1; if(pos>0) return filepathname.substring(pos, filepathname.length()); else return filepathname; } private string getfilepath(string header){ int filenamestart=header.indexof("filename="); int ctypeindex=header.indexof("content-type"); string filename=header.substring(filenamestart, ctypeindex); if ((filename.indexof(´"´)+1)==filename.lastindexof(´"´)) filename=""; else filename=filename.substring(filename.indexof(´"´)+1,filename.lastindexof(´"´)); return filename; } public void setmaxfilesize(int maxvalue){ if(maxvalue>0) this.max_file_size=maxvalue; } }
|
|