首先去http://www.jspsmart.com下載jspSmartUpload.zip,解壓縮后將文件放在WEB-INF/classes/下(目前這個網站已經被關閉)
本地下載
下面是演示。SmartUpLoad只有一個構造函數。即public
SmartUpload在jsp中直接使用javaBean來生成一個SmartUpLoad的對象
<jsp:useBean
id="mySmartUpload" scope="page"
class="com.jspsmart.upload.SmartUpload"/>
下面是演示。其中方法都是有注釋的
------------------------------Jspsmart.html---------------
<
html
>
<
head
>
<
title
>
Jspsmart.html
</
title
>
<
meta?
http-equiv
="Content-Type"
?content
="text/html;?charset=GB2312"
>
</
head
>
<
body
>
<
h2
>
文件上傳范例?-?jspSmart
</
h2
>
<
form?
name
="Form1"
?enctype
="multipart/form-data"
?method
="post"
?action
="Jspsmart.jsp"
>
<
p
>
上傳文件?1:
<
input?
type
="file"
?name
="File1"
?size
="20"
maxlength
="20"
></
p
>
<
p
>
上傳文件?2:
<
input?
type
="file"
?name
="File2"
?size
="20"
maxlength
="20"
></
p
>
<
input?
type
="submit"
?value
="上傳"
>
<
input?
type
="reset"
?value
="清除"
>
</
form
>
</
body
>
</
html
>
------------------------------------Jspsmart.jsp--------------------
<%@?page?import="com.jspsmart.upload.*"?%>
<%@?page?contentType="text/html;charset=GB2312"?%>
<html>
<head>
<title>CH9?-?Jspsmart2.jsp</title>
</head>
<body>
<h2>文件上傳范例?-?jspSmart</h2>
<jsp:useBean?id="mySmartUpload"?scope="page"?class="com.jspsmart.upload.SmartUpload"/>
<%
//計算文件上傳個數
int?count=0;
//SmartUpload的初始化,使用這個jspsmart一定要在一開始就這樣聲明
mySmartUpload.initialize(pageContext);
//依據form的內容上傳
mySmartUpload.upload();
//將上傳的文件一個一個取出來處理
for?(int?i=0;i<mySmartUpload.getFiles().getCount();i++)
{
//取出一個文件
com.jspsmart.upload.File?myFile?=?mySmartUpload.getFiles().getFile(i);
//如果文件存在,則做存檔操作
if?(!myFile.isMissing())?{
//將文件存放于絕對路徑的位置
myFile.saveAs("C:\\upload\\"?+?myFile.getFileName(),
mySmartUpload.SAVE_PHYSICAL);
//顯示此上傳文件的詳細信息
out.println("FieldName?=?"?+?myFile.getFieldName()?+?"<BR>");
out.println("Size?=?"?+?myFile.getSize()?+?"<BR>");
out.println("FileName?=?"?+?myFile.getFileName()?+?"<BR>");
out.println("FileExt?=?"?+?myFile.getFileExt()?+?"<BR>");
out.println("FilePathName?=?"?+?myFile.getFilePathName()?+?"<BR>");
out.println("ContentType?=?"?+?myFile.getContentType()?+?"<BR>");
out.println("ContentDisp?=?"?+?myFile.getContentDisp()?+"<BR>");
out.println("TypeMIME?=?"?+?myFile.getTypeMIME()?+"<BR>");
out.println("SubTypeMIME?=?"?+?myFile.getSubTypeMIME()?+?"<BR>");
count?++;
}
}
//?顯示應該上傳的文件數目
out.println("<BR>"?+?mySmartUpload.getFiles().getCount()?+?"?files?could?be?uploaded.<BR>");
//?顯示成功上傳的文件數目
out.println(count?+?"file(s)?uploaded.");
%>
</body>
</html>