不在这里说明swfupload的原理,因为我自己也不是很清楚,只讲怎么用:
首先,在页面中引用SWFUpload.js ,如
<script type="text/javascript" src="<%=request.getContextPath()%>../../swfupload.js"></script>(添加自己的路径)
然后,初始化SWFUpload 及页面配置,如
var swfu;window.onload = function () {
swfu = new SWFUpload({
upload_url: "<%=request.getContextPath()%>/...Action.do?method=...",
post_params: {"name" : "huliang"},
// File Upload Settings
file_size_limit : "100MB", // 1000MB
file_types :"<%=com.jinjian.funds.application.environment.EnvironmentManager.systemEnv.getEnvironment("UPLOAD_TYPE")%>",
file_types_description : "部分文件",
file_upload_limit : "0",
file_queue_error_handler : fileQueueError,
//文件选择失败后触发的事件,默认方法fileQueueError(fileObjct,errorcode,message)
// file_dialog_start_handler:fileDialogStart,
//打开文件选择窗口是触发的事件,默认方法fileDialogStart,可根据需要重载方法
file_dialog_complete_handler : fileDialogComplete,
//选择窗口关闭时触发事件,默认方法fileDialogComplete
file_queued_handler : fileQueued,
//文件成功选择后触发的事件默认方法fileQueued(fileObject),可根据需求重载方法
// upload_start_handler:uploadStart(fileObject),
//上传开始时触发事件,默认方法uploadStart()
upload_progress_handler : uploadProgress,
//文件上传过程中触发事件,默认方法uploadProgress
upload_error_handler : uploadError,
//文件传输过程中出错触发事件,默认方法uploadError
upload_success_handler : uploadSuccess,
//文件上传完成(仅仅是发送,不管服务器是否操作),默认方法uploadSuccess
upload_complete_handler : uploadComplete,
//文件上传周期完成是触发(不管是否上传成功,都会触发)
// 按钮设置
button_image_url : "swfupload/xpbutton.png", // 按钮图标
button_placeholder_id : "spanButtonPlaceholder",
button_width: 61,
button_height: 22,
// swf设置
flash_url : "swfupload/swfupload.swf",
custom_settings : {
progressTarget : "fsUploadProgress",
cancelButtonId : "btnCancel"
},
// Debug 设置
debug: false
});
}
1、页面显示部分:
Html代码
<body style="background-color: #C0D1E3; padding: 2px;">
-
<div id="content">
<form>
<div
style="display: inline; border: solid 1px #7FAAFF; background-color: #C5D9FF; padding: 2px;">
<span id="spanButtonPlaceholder"></span>
<input id="btnUpload" type="button" value="上 传"
οnclick="startUploadFile();" class="btn3_mouseout" onMouseUp="this.className='btn3_mouseup'"
οnmοusedοwn="this.className='btn3_mousedown'"
onMouseOver="this.className='btn3_mouseover'"
οnmοuseοut="this.className='btn3_mouseout'"/>
<input id="btnCancel" type="button" value="取消所有上传"
οnclick="cancelUpload();" disabled="disabled" class="btn3_mouseout" onMouseUp="this.className='btn3_mouseup'"
οnmοusedοwn="this.className='btn3_mousedown'"
onMouseOver="this.className='btn3_mouseover'"
οnmοuseοut="this.className='btn3_mouseout'"/>
</div>
</form>
<div id="divFileProgressContainer"></div>
<div id="thumbnails">
<table id="infoTable" border="0" width="530" style="display: inline; border: solid 1px #7FAAFF; background-color: #C5D9FF; padding: 2px;margin-top:8px;">
</table>
</div>
</div>
<button id="extfileupload" οnclick="showExtShow();" class="btn3_mouseout" onMouseUp="this.className='btn3_mouseup'"
οnmοusedοwn="this.className='btn3_mousedown'"
onMouseOver="this.className='btn3_mouseover'"
οnmοuseοut="this.className='btn3_mouseout'">show</button>
2、Java处理文件上传部分:
public class FileUploadServlet extends HttpServlet {
private static final long serialVersionUID = -7825355637448948879L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
DiskFileItemFactory factory = new DiskFileItemFactory();
// 设置内存缓冲区,超过后写入临时文件
factory.setSizeThreshold(10240000);
// 设置临时文件存储位置
String base = "E:/upload111";
File file = new File(base);
if(!file.exists())
file.mkdirs();
factory.setRepository(file);
ServletFileUpload upload = new ServletFileUpload(factory);
// 设置单个文件的最大上传值
upload.setFileSizeMax(10002400000l);
// 设置整个request的最大值
upload.setSizeMax(10002400000l);
upload.setHeaderEncoding("UTF-8");
try {
List<?> items = upload.parseRequest(request);
FileItem item = null;
String fileName = null;
for (int i = 0 ;i < items.size(); i++){
item = (FileItem) items.get(i);
fileName = base + File.separator + item.getName();
// 保存文件
if (!item.isFormField() && item.getName().length() > 0) {
item.write(new File(fileName));
}
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}