public class UpLoadedFile {
public static final String DESTINATION_FILE_ALREADY_EXISTS = "文件已存在.";
public static final String DESTINATION_IS_A_DIRECTORY = "The destination is a directory.";
protected java.lang.String strPathName; // 上传文件的路径
protected java.lang.String strFileName; // the upLoaded file name
protected java.lang.String strContentType; // content type of the upLoaded file name
protected java.io.ByteArrayOutputStream bBuffer; // file's content in byte format --- OutputStream.
/**
* file's content in byte formate
* Creation date: (May 16,2001 ) by wxj
* @return java.io.ByteArrayOutputStream
*/
public ByteArrayOutputStream getBuffer() {
return bBuffer;
}
/**
* get file's content type
* @return java.lang.String
*/
public java.lang.String getStrContentType() {
return strContentType;
}
/**
* Save the upLoaded file's content to a local file
* @param strFileName java.lang.String
* destination's full path name.
* @param bOverWrite boolean
* true: overwrite it if the destination already exists.
* false: do not overwrite it.
* @exception java.lang.Exception The exception description.
* Note: if the file's content stream is null, it will create an empty file
* for you.
*/
public void writeToFile(String strFileName, boolean bOverWrite)
throws java.lang.Exception
{
File objFile = new File(strFileName);
if ( objFile.isDirectory() )
throw new Exception( DESTINATION_IS_A_DIRECTORY );
if ( objFile.isFile() && objFile.exists() && !bOverWrite )
throw new Exception( DESTINATION_FILE_ALREADY_EXISTS );
public class UpLoad{
//变量申明
public static final int BUF_SIZE = 1024*400; //一行读入最大字节数
public static final int STREAM_SIZE = 5 * 1024*1024; //文件上限字节数
protected java.util.Hashtable hashElements; // contain the parsed form data filename-->ArrayList
public static final String NAME_STRING = "Content-Disposition: form-data; name=\"";
public static final String CONTENT_TYPE_STRING = "Content-Type: ";
public static final String Encode = "ISO-8859-1";
public static final String split = "\\";
/**分析输入流
*Created by wxj on May 19, 2001
*/
public void parser(HttpServletRequest request) throws IOException{
String firstLine; //读入的第一行内容
String strResult; //读入的内容
String strKey=null; //上传文件变量名
String strContentType=null;
UpLoadedFile upFile =null; //上传文件对象
String strFile = "";
String strPath = "";
int readLen; //读入的字节数
int iNameStrLen = NAME_STRING.length();
ServletInputStream is;
is = request.getInputStream(); //输入流
byte[] b= new byte[BUF_SIZE]; //
boolean bSection = false; //是否开始读取文件字节流
hashElements = new Hashtable();
//保留一组同变量名的UpLoadFile对象
ArrayList values = null;
//读取第一行
readLen = is.readLine(b,0,BUF_SIZE);
firstLine = new String( b, 0, readLen -4);
while (-1 != (readLen = is.readLine(b,0,BUF_SIZE)) ) {
strResult = new String(b,0,readLen);
//读取文件名行
int index;