您的位置:首页 > Web前端 > JavaScript

文件上传之jspSmartUpload

2008-06-04 16:30 344 查看
文件上传之jspSmartUpload
将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>

注意事项:

一般输入类型(例如,text、password、select,等等)传送Form到服务器端时,所用的编码方式是application/x-www-form-urlencoded,但是若要传送文件至服务器端时,必须使用mutilpart/form-data的编码方式;

Jspsmart2.jsp中斜体字部分可以用来重命名上传的文件

jspSmartUpload上传过程对于文件名包含汉字的情况是比较理想的,但是下载时的支持就不太好了。如果供下载的文件名包含汉字,则提示下载的文件名缺省为一串随即的乱码(探索这个问题的解决)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: