Posted on 2008-10-02 23:16
H2O 閱讀(487)
評論(0) 編輯 收藏 所屬分類:
java
此例子是基于jspsmartupload組件的,jspsmartupload是一個不錯的上傳下載組件,但對中文支持不足。若下載的文件名中有漢字,則瀏覽器在提示另存的文件名時,顯示的是一堆亂碼,讓人看了很不舒服,為此,有人專門修改此組件,做了編碼的轉換工作,將文件名轉換為UTF-8形式的編碼形式。我用的是網上修改過的,已經可以支持中文,相信你也可以找到,如果需要,可以聯系我,我會在第一時間發給你!

在網上找了很多相關資料,自己也添加了一些js代碼,基本實現了動態添加刪除多文件上傳的功能,如果想要做得更完美,或者把文件上傳下載信息存儲到數據庫等,那就自己去完善了,以下是所有的源代碼:

(文件下載出于安全考慮是按流的方式來進行的,而不是直接給出文件下載路徑地址,所以像迅雷等下載工具是不能下載的)

首先當然是上傳下載的頁面了,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);
}
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>基于jspsmart upload組件的文件上傳下載</h3>
<form action="servlet/ServletUpload" 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/ServletDownload" method="post">
下載文件的名稱:
<input type="text" name="downloadFileName" size="20" maxlength="80">
<input type="submit" value="下載">
</form>
</body>
</html>
然后是實現上傳和下載的兩個servlet類

上傳文件ServletUpload類:

package 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 ServletUpload extends HttpServlet {

private ServletConfig config;

public ServletUpload() {
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) {
// TODO Auto-generated catch block
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;
}
}

下載文件ServletDownload類:

package 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 ServletDownload extends HttpServlet {

private ServletConfig config;

public ServletDownload() {
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.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ServletUpload</servlet-name>
<servlet-class>servlet.ServletUpload</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ServletDownload</servlet-name>
<servlet-class>servlet.ServletDownload</servlet-class>
</servlet>


<servlet-mapping>
<servlet-name>ServletUpload</servlet-name>
<url-pattern>/servlet/ServletUpload</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ServletDownload</servlet-name>
<url-pattern>/servlet/ServletDownload</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

要實現無刷新上傳其實很簡單。只要利用iframe即可,只要把target設置為隱藏的iframe將就可以了。
<iframe name='hidden_frame' id="hidden_frame" style='display:none'></iframe>