當前位置:才華齋>計算機>java語言>

JSP頁面如何實現檔案上傳下載

java語言 閱讀(3.19W)

Java 技術具有卓越的通用性、高效性、平臺移植性和安全性,廣泛應用於PC、資料中心、遊戲控制檯、科學超級計算機、行動電話和網際網路,同時擁有全球最大的開發者專業社群。下面,小編為大家搜尋整理了JSP頁面如何實現檔案上傳下載,希望能給大家帶來幫助!更多精彩內容請及時關注我們應屆畢業生考試網!

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();