程序如下
package linkmanhelper;
import javaio*;
import javautil*;
import javaxservlet
import javaxservlet
import javaxservletServletInputStream;
public class UpLoadBean {
private String SavePath = null;
private String FileName = null;
private String Sperator = null;
private String ErrMsg = "正常返回";
private byte[] bt = new byte[];
private byte[] prebt = new byte[];
private int t = ;
private long FileSize = ;
private boolean UserDefineFileName = false;
/////////////////////////////////////////////////
public UpLoadBean () {
FileName = bringFileName ();
}
public UpLoadBean (String savepath) {
SavePath = savepath;
FileName = bringFileName ();
}
/////////////////////////////////////////////////
// 文件名信息
private String bringFileName () {
Calendar dt = CalendargetInstance ();
String str = "" + dtget (CalendarYEAR) + dtget (CalendarMONTH) + dtget (CalendarDAY_OF_MONTH);
str = str + dtget (CalendarHOUR) + dtget (CalendarMINUTE) + dtget (CalendarSECOND);
return str;
}
public void setFileName (String filename) {
FileName = filename;
UserDefineFileName = true;
}
public String getFileName () {
return FileName;
}
/////////////////////////////////////////////////
// 上传文件主程序
public boolean UpLoad (HttpServletRequest request) throws Exception {
if (SavePath == null) {
setErrorMessage ("未设置保存上传文件的路径");
return false;
}
requestsetCharacterEncoding("utf");
ServletInputStream in = requestgetInputStream ();
////////////////////////////////////////////
// 获取得一行的分隔标志
t = inreadLine (bt btlength);
if (t != ) {
Sperator = new String (bt t);
Sperator = Speratorsubstring ( );
t = ;
}
////////////////////////////////////////////
// 取扩展名并设定文件全名
do {
t = inreadLine (bt btlength);
String s = new String (bt t);
int index = sindexOf ("filename=\"");
if (index != ) {
s = ssubstring (index + );
index = sindexOf ("\"");
s = ssubstring ( index);
index = slastIndexOf ("");
if (!UserDefineFileName) {
FileName = FileName + ssubstring (index); // 设定文件全名
}
t = ;
}
} while (t != );
////////////////////////////////////////////
// 取文件内容
FileOutputStream out = new FileOutputStream (SavePath + "\\" + FileName);
t = inreadLine (bt btlength);
String s = new String (bt t);
int i = sindexOf ("ContentType:");
if (i == ) {
setErrorMessage ("上传的不是文件");
return false;
} else {
inreadLine (bt btlength); // 去掉一个空行
t = ;
}
boolean state = true;
long trancsize = ;
try {
t = inreadLine (bt btlength);
while (t != ) {
s = new String (bt t);
if (slength () > ) {
s = ssubstring ( );
if (sequals (Sperator)) {
break;
}
}
/*
if (bt[] == && bt[] == && bt[] == && bt[] == && bt[] == ) {
break;
}
*/
if (FileSize != ) {
if (trancsize >= FileSize) {
throw new Exception ("上传文件太大");
}
}
outwrite (bt t);
trancsize += t;
t = inreadLine (bt btlength);
}
} catch (Exception e) {
state = false;
outclose ();
setErrorMessage (egetMessage ());
File tmp = new File (SavePath FileName);
if (!tmpdelete()) {
setErrorMessage (egetMessage () + "清除未完成");
}
} finally {
if (state) {
outclose ();
}
return state;
}
}
/////////////////////////////////////////////////
// 上传文件错误信息
public void setErrorMessage (String errormessage) {
ErrMsg = errormessage;
}
public String getErrorMessage () {
return ErrMsg;
}
/////////////////////////////////////////////////
// 文件大小信息
public void setFileSize (long size) {
FileSize = size;
}
public long getFileSize () {
return FileSize;
}
}// end class