首先,要引几个文件(见附件)
<link href="<%=strPath%>/resources/css/SWFUpload/default.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="<%=strPath%>/resources/js/SWFUpload/swfupload.js"></script> <script type="text/javascript" src="<%=strPath%>/resources/js/SWFUpload/swfupload.queue.js"></script> <script type="text/javascript" src="<%=strPath%>/resources/js/SWFUpload/fileprogress.js"></script> <script type="text/javascript" src="<%=strPath%>/resources/js/SWFUpload/handlers.js"></script>
然后上传的页面,直接粘了
<%@ page language="java" pageEncoding="UTF-8"%> <%String strPath = request.getContextPath();%> <!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>图片批量上传</title> <link href="<%=strPath%>/resources/css/SWFUpload/default.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="<%=strPath%>/resources/js/SWFUpload/swfupload.js"></script> <script type="text/javascript" src="<%=strPath%>/resources/js/SWFUpload/swfupload.queue.js"></script> <script type="text/javascript" src="<%=strPath%>/resources/js/SWFUpload/fileprogress.js"></script> <script type="text/javascript" src="<%=strPath%>/resources/js/SWFUpload/handlers.js"></script> <script type="text/javascript"> var swfu; window.onload = function() { var settings = { flash_url : "<%=request.getContextPath() %>/resources/js/SWFUpload/swfupload.swf", upload_url: "../../om/uploadFile.do?method=picturesUpload", //post_params: {}, file_size_limit : "10 MB", file_types : "*.jpg", file_types_description : "jpg 文件", file_queue_limit : 0, custom_settings : { progressTarget : "fsUploadProgress", cancelButtonId : "btnCancel" }, debug: false, // Button settings button_image_url: "<%=request.getContextPath() %>/resources/js/SWFUpload/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: 16; }", button_text_left_padding: 12, button_text_top_padding: 3, // The event handler functions are defined in 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> </head> <body> <div id="content"> <h2>图片批量上传</h2> <form id="form1" action="" method="post" enctype="multipart/form-data"> <div class="fieldset flash" id="fsUploadProgress"> <span class="legend">上传队列</span> </div> <div id="divStatus">0 个文件已上传</div> <div> <span id="spanButtonPlaceHolder"></span> <input id="btnCancel" type="button" value="取消所有上传" onclick="swfu.cancelQueue();" disabled="disabled" style="margin-left: 2px; font-size: 8pt; height: 29px;" /> </div> <span>注:必须上传.jpg格式图片<br>建议:每次上传不超过30张,每张图片大小不超过10MB。</span> </form> </div> </body> </html>
后台java代码
/** * * 功能:图片批量上传 * @param mapping * @param form * @param request * @param response * @return */ @SuppressWarnings("unchecked") public ActionForward picturesUpload(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { KoofUmUser user = (KoofUmUser) request.getSession().getAttribute("koofumuser"); String userName = user.getLoginname(); response.setContentType("text/html;charset=utf-8"); SmartUpload su = null; try { JspFactory jspxFactory = JspFactory.getDefaultFactory(); PageContext pageContext = jspxFactory.getPageContext(this .getServlet(), request, response, null, true, 8192, true); su = new SmartUpload(); su.initialize(pageContext); su.upload(); String backInfo = UpMgr.fileUpload(su, pageContext); String fPath = new SysUtil().getAttribute("koofglobals.koofhome") + "up/" + backInfo; File myfile = new File(fPath); // 如果文件存在,则导入数据表中 if (myfile.exists() && myfile.isFile()) { String fileName = myfile.getName(); String[] idcards = fileName.split("\\."); String idcard = idcards[0]; //这里我存到了数据库 this.savePhoto(myfile, idcard, userName); } //删除文件 myfile.delete(); //删除文件夹 myfile.getParentFile().delete(); } catch (Exception e) { e.printStackTrace(); } return null; }
基本上就这两块,配置文件都正常配置就行,具体可以看附件