用jspsmartupload实现Jsp多文件上传下载,并解...

此例子是基于jspsmartupload组件的,jspsmartupload是一个不错的上传下载组件,但对中文支持不足。若下载的文件名中有汉字,则浏览器在提示另存的文件名时,显示的是一堆乱码,让人看了很不舒服,为此,有人专门修改此组件,做了编码的转换工作,将文件名转换为UTF-8形式的编码形式。我用的是网上修改过的,已经可以支持中文,相信你也可以找到,如果需要,可以联系我,我会在第一时间发给你!jar down:
http://cid-75be94924ba7fb04.skydrive.live.com/self.aspx/Public/SmartUpload%5E_zh%5E_CN.jar

在网上找了很多相关资料,自己也添加了一些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>基于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;


public class UploadServlet extends HttpServlet {
private ServletConfig config;

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>

你可能感兴趣的:(用jspsmartupload实现Jsp多文件上传下载,并解...)