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]==´
|