Java 技術具有卓越的通用性、高效性、平臺移植性和安全性,廣泛應用於PC、數據中心、遊戲控制檯、科學超級計算機、移動電話和互聯網,同時擁有全球最大的開發者專業社羣。下面,小編爲大家搜索整理了JSP頁面如何實現文件上傳下載,希望能給大家帶來幫助!更多精彩內容請及時關注我們應屆畢業生考試網!
名稱:jsp頁面上傳類
作者:SinNeR
Mail:vogoals[at]
特點:
可以多文件上傳;
返回上傳後的文件名;
form表單中的其他參數也可以得到。
先貼上傳類,JspFileUpload
package ;
import eredOutputStream;
import ;
import OutputStream;
import ception;
import leDateFormat;
import yList;
import ;
import table;
import letInputStream;
import ServletRequest;
/*
* vogoalAPI 1.0
* Auther
* by
* mail:
*/
/**
* JSP上傳文件類
*
* @author SinNeR
* @version 1.0
*/
public class JspFileUpload {
/** request對象 */
private HttpServletRequest request = null;
/** 上傳文件的路徑 */
private String uploadPath = null;
/** 每次讀取得字節的大小 */
private static int BUFSIZE = 1024 * 8;
/** 存儲參數的Hashtable */
private Hashtable paramHt = new Hasptable();
/** 存儲上傳的文件的文件名的ArrayList */
private ArrayList updFileArr = new ArrayList();
/**
* 設定request對象。
*
* @param request
* HttpServletRequest request對象
*/
public void setRequest(HttpServletRequest request) {
est = request;
}
/**
* 設定文件上傳路徑。
*
* @param path
* 用戶指定的文件的上傳路徑。
*/
public void setUploadPath(String path) {
adPath = path;
}
/**
* 文件上傳處理主程序。
*
* @return int 操作結果 0 文件操作成功;1 request對象不存在。 2 沒有設定文件保存路徑或者文件保存路徑不正確;3
* 沒有設定正確的enctype;4 文件操作異常。
*/
public int process() {
int status = 0;
// 文件上傳前,對request對象,上傳路徑以及enctype進行check。
status = preCheck();
// 出錯的時候返回錯誤代碼。
if (status != 0)
return status;
try {
// 參數或者文件名
String name = null;
// 參數的value
String value = null;
// 讀取的流是否爲文件的標誌位
boolean fileFlag = false;
// 要存儲的文件。
File tmpFile = null;
// 上傳的文件的名字
String fName = null;
FileOutputStream baos = null;
BufferedOutputStream bos = null;
// 存儲參數的Hashtable
paramHt = new Hashtable();
updFileArr = new ArrayList();
int rtnPos = 0;
byte[] buffs = new byte[BUFSIZE * 8];
// 取得ContentType
String contentType = ontentType();
int index = xOf("boundary=");
String boundary = "--" + tring(index + 9);
String endBoundary = boundary + "--";
// 從request對象中取得流。
ServletInputStream sis = nputStream();
// 讀取1行
while ((rtnPos = Line(buffs, 0, th)) != -1) {
String strBuff = new String(buffs, 0, rtnPos);
// 讀取1行數據
if (tsWith(boundary)) {
if (name != null && ()th() > 0) {
if (fileFlag) {
h();
e();
e();
baos = null;
bos = null;
(fName);
} else {
Object obj = (name);
ArrayList al = new ArrayList();
if (obj != null) {
al = (ArrayList) obj;
}
(value);
tln(value);
(name, al);
}
}
name = new String();
value = new String();