项目需要实现一个无刷新批量文件上传功能,仔细研究了下,发现JQuery 提供的Uploadify插件十分不错,不过官方的实例是基于php的,下面我用jsp+servlet简单实现了这个功能,废话少说,先看效果图:
1、初始化页面:

2、选择多个文件(可一次多选)后:

3、点击开始上传(上传完就自动消失)

效果就是上面那样,页面不刷新。下面上代码:
1、首先先到官网下载最新的zip压缩包http://www.uploadify.com
2、项目结构:

3、关键代码:
index.jsp
01
|
<%@
page language="java" contentType="text/html; charset=utf-8"%>
|
03
|
String
path = request.getContextPath();
|
04
|
String
basePath = request.getScheme() + "://"
|
05
|
+
request.getServerName() + ":" + request.getServerPort()
|
08
|
<!DOCTYPE
HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
11
|
<basehref="<%=basePath%>">
|
15
|
<linkhref="css/default.css"rel="stylesheet"type="text/css"/>
|
16
|
<linkhref="css/uploadify.css"rel="stylesheet"type="text/css"/>
|
17
|
<scripttype="text/javascript"src="scripts/jquery-1.4.2.min.js"></script>
|
18
|
<scripttype="text/javascript"src="scripts/swfobject.js"></script>
|
19
|
<scripttype="text/javascript"src="scripts/jquery.uploadify.v2.1.4.min.js"></script>
|
22
|
<scripttype="text/javascript">
|
23
|
$(document).ready(function()
{
|
24
|
$("#uploadify").uploadify({
|
25
|
'uploader'
: 'scripts/uploadify.swf',
|
26
|
'script'
: 'servlet/Upload',//后台处理的请求
|
27
|
'cancelImg'
: 'images/cancel.png',
|
28
|
'folder'
: 'uploads',//您想将文件保存到的路径
|
29
|
'queueID'
: 'fileQueue',//与下面的id对应
|
31
|
'fileDesc'
: 'rar文件或zip文件',
|
32
|
'fileExt'
: '*.rar;*.zip', //控制可上传文件的扩展名,启用本项时需同时声明fileDesc
|
36
|
'buttonText'
: 'BROWSE'
|
43
|
<divid="fileQueue"></div>
|
44
|
<inputtype="file"name="uploadify"id="uploadify"/>
|
46
|
<ahref="javascript:jQuery('#uploadify').uploadifyUpload()">开始上传</a>
|
47
|
<ahref="javascript:jQuery('#uploadify').uploadifyClearQueue()">取消所有上传</a>
|
web.xml
01
|
<?xmlversion="1.0"encoding="UTF-8"?>
|
05
|
xmlns="http://java.sun.com/xml/ns/j2ee"
|
07
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
09
|
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
|
11
|
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
|
15
|
<servlet-name>upload</servlet-name>
|
17
|
<servlet-class>com.xzit.upload.Upload</servlet-class>
|
23
|
<servlet-name>upload</servlet-name>
|
25
|
<url-pattern>/servlet/Upload</url-pattern>
|
31
|
<welcome-file>index.jsp</welcome-file>
|
Upload.java
001
|
packagecom.xzit.upload;
|
005
|
importjava.io.IOException;
|
007
|
importjava.util.Iterator;
|
009
|
importjava.util.List;
|
011
|
importjava.util.UUID;
|
015
|
importjavax.servlet.ServletException;
|
017
|
importjavax.servlet.http.HttpServlet;
|
019
|
importjavax.servlet.http.HttpServletRequest;
|
021
|
importjavax.servlet.http.HttpServletResponse;
|
025
|
importorg.apache.commons.fileupload.FileItem;
|
027
|
importorg.apache.commons.fileupload.FileUploadException;
|
029
|
importorg.apache.commons.fileupload.disk.DiskFileItemFactory;
|
031
|
importorg.apache.commons.fileupload.servlet.ServletFileUpload;
|
035
|
@SuppressWarnings("serial")
|
037
|
publicclassUploadextendsHttpServlet
{
|
039
|
@SuppressWarnings("unchecked")
|
041
|
publicvoiddoPost(HttpServletRequest
request, HttpServletResponse response)
|
043
|
throwsServletException,
IOException {
|
045
|
String
savePath =this.getServletConfig().getServletContext()
|
049
|
savePath
= savePath +"/uploads/";
|
051
|
File
f1 =newFile(savePath);
|
053
|
System.out.println(savePath);
|
061
|
DiskFileItemFactory
fac =newDiskFileItemFactory();
|
063
|
ServletFileUpload
upload =newServletFileUpload(fac);
|
065
|
upload.setHeaderEncoding("utf-8");
|
071
|
fileList
= upload.parseRequest(request);
|
073
|
}catch(FileUploadException
ex) {
|
079
|
Iterator<FileItem>
it = fileList.iterator();
|
085
|
while(it.hasNext())
{
|
087
|
FileItem
item = it.next();
|
089
|
if(!item.isFormField())
{
|
091
|
name
= item.getName();
|
093
|
longsize
= item.getSize();
|
095
|
String
type = item.getContentType();
|
097
|
System.out.println(size
+"
"+
type);
|
099
|
if(name
==null||
name.trim().equals(""))
{
|
107
|
if(name.lastIndexOf(".")
>=0)
{
|
109
|
extName
= name.substring(name.lastIndexOf("."));
|
119
|
name
= UUID.randomUUID().toString();
|
121
|
file
=newFile(savePath
+ name + extName);
|
123
|
}while(file.exists());
|
125
|
File
saveFile =newFile(savePath
+ name + extName);
|
129
|
item.write(saveFile);
|
131
|
}catch(Exception
e) {
|
141
|
response.getWriter().print(name
+ extName);
|
相关文章: