適用于JSP,將jspSmartUpload.jar置于WEB-INF\lib下
----------------------------------------------------------
Jspsmart1.html
<html>
<head>
<title>Jspsmart1.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="Jspsmart1.jsp">
<p>上傳文件 1:<input type="file" name="File1" size="20" maxlength="20"></p>
<input type="submit" value="上傳">
<input type="reset" value="清除">
</form>
</body>
</html>
Jspsmart1.jsp
<%@ page import="com.jspsmart.upload.*" %>
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
<title>Jspsmart1.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);
//生命限制上傳的文件大小為 5 MB
mySmartUpload.setMaxFileSize(5 * 1024 * 1024);
//依據form的內容上傳
mySmartUpload.upload();
try {
//將文件存放于D:\totalExample\jsp\UploadFile\
count = mySmartUpload.save("D:\\totalExample\\jsp\\UploadFile\\");
//打印出上傳文件的個數
out.println("您成功上傳"+count + "個文件.");
} catch (Exception e) {
out.println(e.toString());
}
%>
</body>
</html>
---------------------------------------------------------------------
Jspsmart2.html
<html>
<head>
<title>Jspsmart3.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="Jspsmart2.jsp">
<p>上傳文件 1:<input type="file" name="File1" size="20" maxlength="20"></p>
<input type="submit" value="上傳">
<input type="reset" value="清除">
</form>
</body>
</html>
Jspsmart2.jsp
<%@ page import="com.jspsmart.upload.*" %>
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
<title>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("D:\\totalExample\\jsp\\UploadFile\\" + 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>
------------------------------------------------------------------------------
Jspsmart3.html
<html>
<head>
<title>Jspsmart3.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="Jspsmart2.jsp">
<p>上傳文件 1:<input type="file" name="File1" size="20" maxlength="20"></p>
<input type="submit" value="上傳">
<input type="reset" value="清除">
</form>
</body>
</html>
Jspsmart3.jsp
<%@ page import="com.jspsmart.upload.*" %>
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
<title>Jspsmart3.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);
//聲明可以上傳的文件類型
mySmartUpload.setAllowedFilesList("htm,html,txt,,");
//限制存檔位置,可存檔于絕對位置
mySmartUpload.setDenyPhysicalPath(false);
//依據 form之內容上傳
mySmartUpload.upload();
//將文件用原本的名字存放于server上的相對路徑
try {
count = mySmartUpload.save("D:\\totalExample\\jsp\\UploadFile\\", mySmartUpload.SAVE_PHYSICAL);
} catch (Exception e) {
out.println("<b>Wrong selection : </b>" + e.toString());
}
//打印出總共上傳文件個數
out.println(count + " file(s) uploaded.");
%>
</body>
</html>
---------------------------------------------------------------
download.jsp
<%@ page import="com.jspsmart.upload.*" %>
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
<title> download.jsp</title>
</head>
<body>
<h2>文件下載范例 - jspSmart</h2>
<jsp:useBean id="mySmartUpload" scope="page"
class="com.jspsmart.upload.SmartUpload" />
<%
// SmartUpload之初始化
mySmartUpload.initialize(pageContext);
//必須如此聲明,否則將會把文件顯示于瀏覽器中
mySmartUpload.setContentDisposition("inline;");
//將 sample.zip下載,下載默認名稱為downloaded.zip
mySmartUpload.downloadFile("C:\\upload\\sample.zip",
"application/x-zip-compressed",
"downloaded.zip");
%>
</body>
</html>
J-CN工作室
www.j-cn.org