注意~~~~~~~~`:
《1》使用之前需要自己下載jspSmartUpload.jar包 這裏找到一個支持中文的jar包,下載地址如下: http://www.blogjava.net/Files/hijackwust/jsmartcom_zh_CN.rar
把壓縮包裏面的jar拷貝到工程的lib下面即可
《2》在項目的webRoot下新建一個文件夾(uploadfile),文件夾裏新建一個文件(test1.txt)
~~~~~~~~~~~~~~~~~~~~~~~~~~文件的上傳~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="com.jspsmart.upload.*"%>
<jsp:useBean id="SU" scope="page" class="com.jspsmart.upload.SmartUpload" />
<html>
<head>
<title>上載附件 </title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<center>正在上傳文件...</center>
<%
//上載附件
try
{
SU.initialize(pageContext); //上傳初始化。
SU.service(request,response);
SU.upload(); //上傳文件。
SU.setAllowedFilesList("doc,txt"); // 設定允許上傳的文件(通過擴展名限制),僅允許doc,txt文件。
SU.setDeniedFilesList("exe,bat,jsp,htm,html,,"); //設定禁止上傳的文件(通過擴展名限制),禁止上傳帶有exe,bat, jsp,htm,html擴展名的文件和沒有擴展名的文件。
SU.setMaxFileSize(10000); // 限制每個上傳文件的最大長度。
SU.setTotalMaxFileSize(20000); // 限制總上傳數據的長度。
String fn=SU.getFiles().getFile(0).getFileName(); //獲得上傳名稱
SU.save("uploadfile/");//文件保存的目錄爲UploadDir
out.println("<br>成功上傳文件,請查看 uploadfile/"+fn+"文件<br>確認文件是否上傳成功!");
}
catch(Exception ex)
{
ex.printStackTrace();
}
%>
<a href="UpLoad.html"><div>重新上傳文件</div></a>
</body>
</html>
<html>
<head>
<title>文件上傳</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<p align="center">上傳文件選擇</p>
<FORM method="POST" action="UpLoad.jsp" enctype="multipart/form-data">
<input type="hidden" name="TEST" value="good">
<table width="75%" border="1" align="center">
<tr>
<td><div align="center">1:
<input type="FILE" name="FILE1" size="30">
</div></td>
</tr>
<tr>
<td><div align="center">
<input type="submit" name="Submit" value="上傳它!">
</div></td>
</tr>
</table>
</FORM>
</body>
</html>
~~~~~~~~~~~~~~~~~~~~~~~~文件的下載~~~~~~~~~~~~~~
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="com.jspsmart.upload.*"%>
<jsp:useBean id="SU" scope="page" class="com.jspsmart.upload.SmartUpload" />
<html>
<head><title></title></head>
<body>
<%
// 初始化
SU.initialize(pageContext);
//SU.service(request,response);
// 設定contentDisposition爲null以禁止瀏覽器自動打開文件,
//保證點擊鏈接後是下載文件。若不設定,則下載的文件擴展名爲
//doc時,瀏覽器自動用Word打開它。擴展名爲pdf時,瀏覽器用Acrobat打開。自動進行關聯。
SU.setContentDisposition(null);
//下載文件
SU.downloadFile("/uploadfile/test1.txt");
out.clear();
out=pageContext.pushBody();
%>
</body>
</html>
<html>
<head>
<title>下載</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<a href="DownLoad.jsp"><h2 align="center">點擊下載</h2></a>
</body>
</html>