程序如下
package linkman
import java
import java
import javax
import javax
import javax
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 = Calendar
String str = "" + dt
str = str + dt
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;
}
request
ServletInputStream in = request
////////////////////////////////////////////
// 獲取得一行的分隔標志
t = in
if (t !=
Sperator = new String (bt
Sperator = Sperator
t =
}
////////////////////////////////////////////
// 取擴展名並設定文件全名
do {
t = in
String s = new String (bt
int index = s
if (index !=
s = s
index = s
s = s
index = s
if (!UserDefineFileName) {
FileName = FileName + s
}
t =
}
} while (t !=
////////////////////////////////////////////
// 取文件內容
FileOutputStream out = new FileOutputStream (SavePath + "\\" + FileName);
t = in
String s = new String (bt
int i = s
if (i ==
setErrorMessage ("上傳的不是文件");
return false;
} else {
in
t =
}
boolean state = true;
long trancsize =
try {
t = in
while (t !=
s = new String (bt
if (s
s = s
if (s
break;
}
}
/*
if (bt[
break;
}
*/
if (FileSize !=
if (trancsize >= FileSize) {
throw new Exception ("上傳文件太大");
}
}
out
trancsize += t;
t = in
}
} catch (Exception e) {
state = false;
out
setErrorMessage (e
File tmp = new File (SavePath
if (!tmp
setErrorMessage (e
}
} finally {
if (state) {
out
}
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
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20210.html