步骤一: 到官网上下载uploadify的JS文件.
Uploadify在线演示:在线Demo
Uploadify配置参数及接口文档:http://www.uploadify.com/documentation
Uploadify插件下载地址:http://www.uploadify.com/download
--------------------------------------------------------------------
步骤二:下载好uploadify压缩包文件后,解压文件包。在文件夹中找到以下五个文件,并添加到项目的对应路径中:
jquery.uploadify.v2.1.0.js
swfobject.js
uploadify.swf
uploadify.css
cancel.png
步骤三:加入struts2的jar包、struts.xml ,在web.xml中加入struts2的FilterDispatcher过滤器。
----------------------------------------------------------------------------------------------------------------------------------
1、jsp代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<link href="css/default.css" rel="stylesheet" type="text/css" />
<link href="css/uploadify.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="js/swfobject.js"></script>
<script type="text/javascript" src="js/jquery.uploadify.v2.1.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#uploadify").uploadify({
'uploader' : 'uploadify.swf', //是组件自带的flash,用于打开选取本地文件的按钮
'script' : 'uploadAction!uploadFile.action',//处理上传的路径,这里使用Struts2是XXX.action
'cancelImg' : 'image/cancel.png',//取消上传文件的按钮图片,就是个叉叉
'folder' : 'uploads',//上传文件的目录
'fileDataName' : 'uploadify',//和input的name属性值保持一致就好,Struts2就能处理了
'queueID' : 'fileQueue',
'auto' : false,//是否选取文件后自动上传
'multi' : true,//是否支持多文件上传
'simUploadLimit' : 2,//每次最大上传文件数
'buttonText' : 'BROWSE',//按钮上的文字
'displayData' : 'speed',//有speed和percentage两种,一个显示速度,一个显示完成百分比
'fileDesc' : '支持格式:jpg/gif/jpeg/png/bmp.', //如果配置了以下的'fileExt'属性,那么这个属性是必须的
'fileExt' : '*.jpg;*.gif;*.jpeg;*.png;*.bmp',//允许的格式
'onComplete' : function (event, queueID, fileObj, response, data){
$("#result").html(response);//显示上传成功结果
setInterval("showResult()",2000);//两秒后删除显示的上传成功结果
}
});
});
function showResult(){//删除显示的上传成功结果
$("#result").html("");
}
function uploadFile(){//上传文件
jQuery('#uploadify').uploadifyUpload();
}
function clearFile(){(){//清空所有上传队列
jQuery('#uploadify').uploadifyClearQueue();
}
</script>
</head>
<body>
<div id="fileQueue"></div>
<input type="file" name="uploadify" id="uploadify" />
<p>
<a href="javascript:uploadFile()">开始上传</a>
<a href="javascript:clearFile()">取消所有上传</a>
</p>
<div id="result"></div><!--显示结果-->
</body>
</html>
----------------------------------------------------------------------------------------------------------------------------------
2、web.xml
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
----------------------------------------------------------------------------------------------------------------------------------
3、struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2" extends="struts-default">
<action name="uploadAction" class="com.lijigou.action.UploadAction" method="uploadFile">
</action>
</package>
</struts>
----------------------------------------------------------------------------------------------------------------------------------
4、action代码
package com.lijigou.action;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class UploadAction extends ActionSupport {
private File uploadify;
private String uploadifyFileName;
@SuppressWarnings("deprecation")
public String uploadFile() throws Exception {
String extName = "";//扩展名
String newFileName= "";//新文件名
String nowTime = new SimpleDateFormat("yyyymmddHHmmss").format(new Date());//当前时间
String savePath = ServletActionContext.getRequest().getRealPath("");
savePath = savePath +"/uploads/";
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
//获取扩展名
if(uploadifyFileName.lastIndexOf(".")>=0){
extName = uploadifyFileName.substring(uploadifyFileName.lastIndexOf("."));
}
newFileName = nowTime+extName;
uploadify.renameTo(new File(savePath+newFileName));
response.getWriter().print(uploadifyFileName+"上传成功");
return null; //这里不需要页面转向,所以返回空就可以了
}
public File getUploadify() {
return uploadify;
}
public void setUploadify(File uploadify) {
this.uploadify = uploadify;
}
public String getUploadifyFileName() {
return uploadifyFileName;
}
public void setUploadifyFileName(String uploadifyFileName) {
this.uploadifyFileName = uploadifyFileName;
}
}
以上代码都通过测试,没有问题的
参数表:
1 | uploader | 上传控件的主体文件,flash控件 |
2 | script | 相对路径的后端脚本,它将处理您上传的文件。 绝对路径前缀或'/'或'http'的路径 |
3 | checkScript | 检查该文件是否已经选择驻留在服务器上。 |
4 | scriptData | 可提供URL传递参数。用来传递get参数。例如: 'script': 'index.jsp', 'scriptData':{'id':1,'action':'uploadify'}, 注:要设置‘method’:‘GET’. |
5 | fileDataName | |
6 | method | |
7 | | |
8 | folder | 您想将文件保存到的路径。考虑到安全问题,一般并不在客户端设定后供服务器得到所存的路径。我试了下。这个参数好像以get的方式传递的。设定post得不到这个值。 |
9 | queueID | |
10 | queueSizeLimit | 限制在一次队列中的次数(可选定几个文件)。默认值= 999,而一次可传几个文件有 simUploadLimit属性决定。 |
11 | multi | 是否允许同时上传多文件,可设定true或false。 |
12 | auto | 选定文件后是否自动上传,可设定true或false。 |
13 | fileDesc | 出现在上传对话框中的文件类型描述。与fileExt需同时使用 |
14 | fileExt | 支持的格式,启用本项时需同时声明fileDesc。 如:‘*.rar,*.doc |
15 | sizeLimit | 控制上传文件的大小,单位byte |
16 | simUploadLimit | 多文件上传时,同时上传文件数目限制。默认1 |
17 | buttonText | 默认按钮的名字。默认BROWER |
18 | | 使用图片按钮,设定图片的路径即可。 |
19 | hideButton | 上传按钮的隐藏。true 或false。默认flase |
20 | rollover | |
21 | width | |
22 | height | |
23 | wmode | |
24 | cancelImg | |
25 | onInit | 函数, 初始化时的状态。 onInit: function() { $("#id").html("上传前");}, |
26 | onComplete | event: 事件对象 queueID: 完成文件的唯一标识符。 fileObj: response: 服务器回调的数据 data: 如: onComplete: function(event, queueID, fileObj) { |
27 | onSelectOnce | 函数:可传递二个参数 event: The event object. data: An object containing details about the select operation. |
28 | onCancel | 函数:可传递四个参数 event: The event object. queueID: The unique identifier of the file that was cancelled. fileObj: An object containing details about the file that was selected. data: Details about the file queue. |
29 | onClearQueue | 函数:可传递一个参数 event: The event object. |
30 | onQueueFull | 函数:可传递二个参数 ? event - The event object. ? queueSizeLimit - The maximum size of the queue. |
31 | onError | event: The event object. queueID: The unique identifier of the file that was errored. fileObj: An object containing details about the file that was selected. errorObj: An object containing details about the error returned. |
32 | onOpen | 函数:可传递三个参数 event: The event object. queueID: The unique identifier of the file that was opened. fileObj: An object containing details about the file that was selected. |
33 | onProgress | 函数:可传递四个参数 event: The event object. queueID: The unique identifier of the file that was updated. fileObj: An object containing details about the file that was selected. data: An object containing details about the upload and queue. |
34 | onSelect | event: The event object. queueID: The unique identifier of the file that was selected. fileObj: An object containing details about the file that was selected. |
35 | onAllComplete | event: The event object. data: An object containing details about the upload process. |
36 | onCheck | 函数:可传递五个参数 event: The event object. checkScript: The path to the file checking script. fileQueue: A file queue object consisting of folder: The path to the upload folder. single: True if only one file is being uploaded from the queue. |