swfupload + commons-fileupload实现文件批量上传,带百分比进度
swfupload是一个操纵flash上传文件的javascript库,提供客户端实现,官网:http://code.google.com/p/swfupload/ fileupload是文件上传的服务器端实现,官网:http://commons.apache.org/fileupload/ 本文的环境是在eclipse中配置好本机上的tomcat7上实现。具体细节参见源代码。 有几处加以说明: 1> 这里的实现是在点击"upload"选择文件以后自动上传,如果想自己控制上传,可以将handlers.js中的this.startUpload注释掉,同时在index.jsp中初始化SWFupload的custom_settings中加一个类似于cancelButtonId的startButton按钮,然后在下面html里增加一个相对应的startButton,并且onclick="upload.startUpload();" 这样可以通过点击这个按钮来实现文件上传。 2> 原有的上传进度条是没有百分比的,其设置在handlers.js中的uploadProgress函数中,修改Line 130为progress.setStatus("Uploading...("+percent+" %)"); 3> swfupload在firefox等有些浏览器中无法带上session,因此如果页面需要带有session信息,那么只能手动指定,可以在index.jsp中先得到sessionID: <% String sessionID = session.getId(); System.out.println("session is:" + sessionID); %> ,然后在js中调用: upload_url: "<%=request.getContextPath()%>/upload.jsp;jsessionid=" + '<%=sessionID%>', 4> 参数传递:如果index.jsp在提交页面的时候希望传递一些参数,比如sbmtForm=myForm,那么可以: post_params: { "sbmtForm": 'myForm' },? 源码:http://download.csdn.net/detail/sundongsdu/4642497 两个主要文件列此: index.jsp <%@ page contentType="text/html;charset=utf-8" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>SWFUpload Demos - External Interface Demo</title> <% String sessionID = session.getId(); System.out.println("session is:" + sessionID); %> <link href="swfupload/default.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="swfupload/swfupload.js"></script> <script type="text/javascript" src="swfupload/fileprogress.js"></script> <script type="text/javascript" src="swfupload/handlers.js"></script> <script type="text/javascript"> var upload; window.onload = function() { var settings = { flash_url : "<%=request.getContextPath()%>/swfupload/swfupload.swf",upload_url: "<%=request.getContextPath()%>/upload.jsp;jsessionid=" + '<%=sessionID%>',post_params: { "sbmtForm": 'myForm' },use_query_string : true,file_size_limit : "100 MB",file_types : "*.*",file_types_description : "All Files",file_upload_limit : 10,file_queue_limit : 0,custom_settings : { progressTarget : "fsUploadProgress",cancelButtonId : "btnCancel",// startButton:"startButton",percentage:"percentage" },debug: false,// Button Settings button_image_url : "<%=request.getContextPath()%>/swfupload/XPButtonUploadText_61x22.png",// Relative to the SWF file button_placeholder_id : "spanButtonPlaceHolder",button_width: 61,button_height: 22,// The event handler functions are defined in handlers.js file_dialog_start_handler : fileDialogStart,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 }; upload = new SWFUpload(settings); }; </script> </head> <body> <div id="content"> <form id="form1" method="post" enctype="multipart/form-data"> <div class="fieldset flash" id="fsUploadProgress"> <span class="legend">Upload Queue</span> </div> <div style="padding-left: 5px;"> <span id="spanButtonPlaceHolder"></span> <input id="btnCancel" type="button" value="Cancel" onclick="cancelQueue(upload);" disabled="disabled" style="margin-left: 2px; height: 22px; font-size: 8pt;"/> </div> </form> </div> </body> </html> upload.jsp: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="org.apache.commons.fileupload.servlet.*" %> <%@ page import="org.apache.commons.fileupload.*" %> <%@ page import="org.apache.commons.fileupload.disk.*" %> <%@ page import="org.apache.commons.io.*" %> <%@ page import="java.util.*" %> <%@ page import="java.io.*" %> <% if (ServletFileUpload.isMultipartContent(request)) { try { DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(1024*400); File tmpFile = new File(this.getServletContext().getRealPath("/") + "WEB-INF/tmpfile"); if (!tmpFile.exists()) FileUtils.forceMkdir(tmpFile); factory.setRepository(tmpFile); ServletFileUpload u = new ServletFileUpload(factory); u.setHeaderEncoding("utf-8"); u.setSizeMax(1024*1024*400); String uploadPath = this.getServletContext().getRealPath("/") + "WEB-INF/uploadfile"; File f = new File(uploadPath); if (!f.exists()) FileUtils.forceMkdir(f); List list = u.parseRequest(request); Iterator it = list.iterator(); FileItem item = null; while (it.hasNext()) { item = (FileItem) it.next(); if (!item.isFormField()) { String filename = uploadPath + "/" + item.getName(); item.write(new File(filename)); } } } catch (Exception e){ e.printStackTrace(); } } %> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |