很好的实例:
官方网站:
1. 第一步:需要引入的css
<link href="css/default.css" rel="stylesheet" type="text/css" />
2. 第二步:需要引入的js
<script type="text/javascript" src="js/swfupload.js"></script>
<script type="text/javascript" src="js/swfupload.queue.js"></script>
<script type="text/javascript" src="js/fileprogress.js"></script>
<script type="text/javascript" src="js/handlers.js"></script>
3.第三步:页面设置的swfupload控件的相关属性:
<script type="text/javascript">
var swfu;
window.onload = function() {
var settings = {
flash_url : "js/swfupload.swf", // 调用的上传的flash控件
upload_url: "${pageContext.request.contextPath}/personcenter/axtxcc/doUpload.action", // 后台调用的文件处理程序 注意:配置不正确的话,易导致404错误
file_size_limit : "1024 MB", // 所选文件的最大值
file_types : "*.*", // 限定所上传文件的类型
file_types_description : "All Files",
file_upload_limit : 100, // 设置所上传文件最大个数
file_queue_limit : 0, // 设置上传列表中的最大个数 0:不限制
custom_settings : { // 用户自定义设置
progressTarget : "fsUploadProgress",
uploadButtonId : "btnUpload",
cancelButtonId : "btnCancel"
},
debug: false,
auto_upload:false, // 设置自动上传为false
// Button settings 按钮的设置
button_image_url: "images/TestImageNoText_65x29.png",
button_width: "65",
button_height: "29",
button_placeholder_id: "spanButtonPlaceHolder",
button_text: '<span class="theFont">选择</span>',
button_text_style: ".theFont { font-size: 14; }",
button_text_left_padding: 15,
button_text_top_padding: 3,
// The event handler functions are defined in handlers.js 事件的处理函数定义在handlers.js中
file_queued_handler : fileQueued,
file_queue_error_handler : fileQueueError,
file_dialog_complete_handler : fileDialogComplete,
upload_start_handler : uploadStart,
upload_progress_handler : uploadProgress,
upload_error_handler : uploadError,
upload_success_handler : uploadSuccess,
upload_complete_handler : uploadComplete,
queue_complete_handler : queueComplete // Queue plugin event
};
swfu = new SWFUpload(settings);
};
</script>
4. 第四步:页面代码:
<body>
<form id="form1" action="uploadFile.do" method="post"
enctype="multipart/form-data">
<div class="wrap" id="content">
<div class="titleDiv">
<div class="titleFont">
<img src="images/up.png" width="16" height="16" />
文件上传
</div>
</div>
<div class="content">
<div class="fieldset flash" id="fsUploadProgress">
<p>
<span class="legend">上传列表</span>
</p>
</div>
<div id="divStatus" class="num">
0 个文件上传
</div>
<div style="margin-left: 30px;">
<span id="spanButtonPlaceHolder"></span>
<div style="margin-top: -20px; margin-left: 60px;">
<input id="btnUpload" type="button" value="上 传"
οnclick="swfu.startUpload();"
style="margin-left: 20px; margin-bottom: 10px" class="Btn" />
<input id="btnCancel" type="button" height="29" value="取消上传"
οnclick="swfu.cancelQueue();"
style="margin-left: 20px; margin-bottom: 10px" />
</div>
</div>
</div>
</div>
</form>
</body>
5:第五步:后台的处理代码:
// 上传文件,并将文件信息保存到数据库中
public String doUpload() {
try {
if (file != null) {
// 将文件名解码
/*ServletActionContext.getRequest().setCharacterEncoding("gbk");
String fileFName = ServletActionContext.getRequest()
.getParameter("fileFName");
String title =URLDecoder.decode(fileFName, "UTF-8");
*/
String realpath = ServletActionContext.getServletContext().getRealPath(StringContonts.FILE_AXT_PATH);
// 将文件上传到文件服务器上 begin
File savefile = new File(new File(realpath), fileFileName);
// 判断目录是否存在,不存在,创建目录
if (!savefile.getParentFile().exists())
savefile.getParentFile().mkdirs();
FileUtils.copyFile(file, savefile);
// 将文件上传到文件服务器上 end
return "success";
} else {
return "error";
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "error";
}
}
6. 第六步 配置文件:
<!-- swf文件上传测试 注意:result的返回路径一定要配置正确,否则报404错误 -->
<action name="doUpload" class="swfUploadAction" method="doUpload">
<result name="success">/personcenter/aixuetang/course/swfupload.jsp</result>
<result name="error">/personcenter/aixuetang/course/swfupload.jsp</result>
</action>