此例子是基於jspsmartupload組件的,jspsmartupload是一個不錯的上傳下載組件,但對中文支持不足。若下載的文件名中有漢字,則瀏覽器在提示另存的文件名時,顯示的是一堆亂碼,讓人看了很不舒服,爲此,有人專門修改此組件,作了編碼的轉換工做,將文件名轉換爲UTF-8形式的編碼形式。我用的是網上修改過的,已經能夠支持中文,相信你也能夠找到,若是須要,能夠聯繫我,我會在第一時間發給你!jar down:
http://cid-75be94924ba7fb04.skydrive.live.com/self.aspx/Public/SmartUpload%5E_zh%5E_CN.jarjavascript
在網上找了不少相關資料,本身也添加了一些js代碼,基本實現了動態添加刪除多文件上傳的功能,若是想要作得更完美,或者把文件上傳下載信息存儲到數據庫等,那就本身去完善了,如下是全部的源代碼:html
(文件下載出於安全考慮是按流的方式來進行的,而不是直接給出文件下載路徑地址,因此像迅雷等下載工具是不能下載的)java
首先固然是上傳下載的頁面了,upfile.jsp
<%@ page contentType="text/html;charset=GBK"%>
<html>
<head>
<title>File Upload</title>
<script type="text/javascript">
function addFile(){
var upFile = '<input type="file" name="file1"><br>';
document .getElementById ("files").insertAdjacentHTML("beforeEnd",upFile);
}web
function deleteFile(){
var file = document .getElementById ("files").lastChild;
if(file == null)
return;
document .getElementById ("files").removeChild(file);
file = document .getElementById ("files").lastChild; //移除換行符<br>因此要移兩次
document .getElementById ("files").removeChild(file); //若是在表格裏面不加<br>就自動換行的,能夠去掉,本身把握
}
</script>
</head>
<body>
<h3>基於jsp smart upload組件的文件上傳下載</h3>
<form action="servlet/UploadServlet" method="post" enctype="multipart/form-data">
選擇文件:<div id="files"><input type="file" name="file1"><br></div>
<input type="submit" value="上傳">
<input type="button" value="增長文件" onclick="addFile()">
<input type="button" value="刪除文件" onclick="deleteFile()">
<input type="reset" value="重置">
</form>
<br>
<form action="servlet/DownloadServlet" method="post">
下載文件的名稱:
<input type="text" name="downloadFileName" size="20" maxlength="80">
<input type="submit" value="下載">
</form>
</body>
</html>
上傳文件UploadServlet類:
package com.xml.servlet;數據庫
import java.io.IOException;
import java.io.PrintWriter;瀏覽器
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;安全
import com.jspsmart.upload.SmartUpload;app
public class UploadServlet extends HttpServlet {
private ServletConfig config;jsp
public UploadServlet() {
super();
}工具
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int count = 0; // 記錄文件上傳總個數
SmartUpload mySmartUpload = new SmartUpload();
mySmartUpload.initialize(config, request, response);
try {
// mySmartUpload.setAllowedFilesList("rar,htm,html,jar");//設置容許上傳的文件
mySmartUpload.setDeniedFilesList("exe,jsp,asp");// 禁止上傳的文件
mySmartUpload.setDenyPhysicalPath(true); // 拒絕物理路徑
mySmartUpload.setMaxFileSize(5000000);// 設置容許上傳文件最大爲50000bytes
mySmartUpload.setTotalMaxFileSize(50000000);// 一次上傳文件大小最多不超過5000000bytes
mySmartUpload.upload();
for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
String fileName = myFile.getFileName();
System.out.println("文件名:" + fileName);
}
count = mySmartUpload.save("/upload");
System.out.println(count + "文件已上傳");
} catch (Exception e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
}
下載文件DownloadServlet類:
package com.xml.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
public class DownloadServlet extends HttpServlet {
private ServletConfig config;
public DownloadServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String temp_fileName = request.getParameter("downloadFileName");
if (temp_fileName == null || temp_fileName == "")
return;
byte[] temp_t = temp_fileName.getBytes("ISO8859_1");
String fileName = new String(temp_t, "GBK");
SmartUpload mySmartUpload = new SmartUpload();
mySmartUpload.initialize(config, request, response);
mySmartUpload.setContentDisposition(null);
/*
* 原型:public void setContentDisposition(String contentDisposition)
* 其中,contentDisposition爲要添加的數據。
* 若是contentDisposition爲null,則組件將自動添加"attachment;",
* 以代表將下載的文件做爲附件,結果是IE瀏覽器將會提示另存文件,而不是自動打開這個文件
* (IE瀏覽器通常根據下載的文件擴展名決定執行什麼操做,擴展名爲doc的將用word程序打開,
* 擴展名爲pdf的將用acrobat程序打開,等等)。
*/
try {
mySmartUpload.downloadFile("/upload/" + fileName);
} catch (SmartUploadException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
}
web.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>UploadServlet</servlet-name>
<servlet-class>com.xml.servlet.UploadServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>DownloadServlet</servlet-name>
<servlet-class>com.xml.servlet.DownloadServlet</servlet-class>
</servlet>
<servlet-mapping> <servlet-name>UploadServlet</servlet-name> <url-pattern>/servlet/UploadServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>DownloadServlet</servlet-name> <url-pattern>/servlet/DownloadServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>