SWFUpload综合运用。
发布时间:2020-12-15 17:23:51 所属栏目:百科 来源:网络整理
导读:1.首先引入swfupload 必须得js,和处理各项动作绑定的事件所在的js,(这个js是自己写的 script type="text/javascript" src="fileprogress.js"/scriptscript type="text/javascript" src="handlers.js"/scriptscript type="text/javascript" src="swfupload.js
1.首先引入swfupload 必须得js,和处理各项动作绑定的事件所在的js,(这个js是自己写的 <script type="text/javascript" src="fileprogress.js"></script> <script type="text/javascript" src="handlers.js"></script> <script type="text/javascript" src="swfupload.js"></script>2.初始化swfupload参数,利用参数初始化swf对象 <script language="javascript"> jQuery(function() { var xm_htglfj_setting = { id : "xm_htglfj",flash_url : "/shxxeps/assets/4c2fc69dc91c885837ce55d03493a5f5/com/zzxy/common/components/swfuploader/swfupload.swf",upload_url: "/shxxeps/servlet/UploadServlet",file_size_limit : "200 MB",file_types : "*.*",file_types_description : "文件列表",file_upload_limit : 0,file_queue_limit : 0,use_query_string : true,post_params : { path : "/u01/webfile/xm_htgl_fj/",tableName : "xm_cght",columnName : "htfj",scr : "null",groupID : "1969724d6aac42c8b45f670ed953a862" },custom_settings : { progressTarget : "xm_htglfj_div",delFileAction: "/shxxeps/servlet/UploadServlet",name : "xm_htglfj" },button_image_url: "/shxxeps/assets/5d904f047b37404062ec450c8d94a599/com/zzxy/common/components/swfuploader/image/uploadBtn.png",button_width: "180",button_height: "18",button_placeholder_id: "xm_htglfj_btn",button_text: "<span class='button'>上传附件</span>",button_text_style: ".button { font-family: Helvetica,Arial,sans-serif; font-size: 12pt; }",button_text_left_padding: 18,button_text_top_padding: 0,button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,button_cursor: SWFUpload.CURSOR.HAND,file_queued_handler : fileQueued,file_queue_error_handler : fileQueueError,upload_start_handler : uploadStart,upload_progress_handler : uploadProgress,upload_error_handler : uploadError,upload_success_handler : uploadSuccess,file_dialog_start_handler : fileDialogStart,file_dialog_complete_handler: fileDialogComplete,init_uploaded_files_handler: initUploadedFiles,debug: false }; var xm_htglfj_uploader = new SWFUpload(xm_htglfj_setting); }); </script>3.servlet 处理上传请求。 public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { String action = request.getParameter("action"); if(StringUtils.isNotBlank(action) && action.equals("delete")) { String filePath = request.getParameter("filePath"); File file = new File(Constants_core.GG_TEMP_PATH + filePath); if(file.exists()) { if(file.delete()) { this.ajaxOutPutText(response,"success"); } else { this.ajaxOutPutText(response,"文件删除失败"); } } else { this.ajaxOutPutText(response,"success"); } } else { DiskFileItemFactory factory = new DiskFileItemFactory(); // 设置内存缓冲区,超过后写入临时文件 factory.setSizeThreshold(10240000); // 设置临时文件存储位置 String base = Constants_core.GG_TEMP_PATH; 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 { long fileSize = 0; List items = upload.parseRequest(request); FileItem item = null; String fileName = null; String fileType = null; String realName = null; Map<String,String> paraMap = new HashMap<String,String>(); paraMap.put("path",request.getParameter("path")); paraMap.put("tableName",request.getParameter("tableName")); paraMap.put("columnName",request.getParameter("columnName")); paraMap.put("scr",request.getParameter("scr")); paraMap.put("groupID",request.getParameter("groupID")); for (int i = 0; i < items.size(); i++) { item = (FileItem) items.get(i); // 保存文件 if (!item.isFormField() && item.getName().length() > 0) { fileSize = item.getSize(); realName = URLDecoder.decode(item.getName(),"UTF-8"); fileType = realName.substring(realName.lastIndexOf(".")); fileName = UUID.randomUUID().toString().replaceAll("-","") + fileType; item.write(new File(base + fileName)); } else if(item.isFormField()) { paraMap.put(item.getFieldName(),item.getString()); } } JSONObject obj = new JSONObject(); obj.put("fileName",fileName); obj.put("realName",realName); obj.put("fileSize",fileSize); obj.put("fileType",fileType); obj.put("path",paraMap.get("path")); obj.put("tableName",paraMap.get("tableName")); obj.put("columnName",paraMap.get("columnName")); obj.put("scr",paraMap.get("scr")); obj.put("filePath",base + fileName); String fileGroupID = paraMap.get("groupID"); if(StringUtils.isNotBlank(fileGroupID)) { obj.put("groupID",fileGroupID); } this.ajaxOutPutJson(response,obj.toString()); } catch (FileUploadException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } private void ajaxOutPutJson(HttpServletResponse response,Object outputString) throws IOException { response.setCharacterEncoding("UTF-8"); response.setContentType("application/json"); PrintWriter writer = response.getWriter(); writer.write(outputString == null ? StringUtils.EMPTY : outputString.toString()); } private void ajaxOutPutText(HttpServletResponse response,Object outputString) throws IOException { response.setCharacterEncoding("UTF-8"); response.setContentType("text/plain"); PrintWriter writer = response.getWriter(); writer.write(outputString == null ? StringUtils.EMPTY : outputString.toString()); }4.如何接受返回的值,在自己写的处理js中 processCancel.click(function () { $.ajax({ type: "POST",url: delFileAction,cache: false,dataType: "text",data: {filePath: fileName,action: "delete"},success:function(msg){ if(msg=="success"){ this_.setCancelled(); this_.delFile(swfUploadInstance); }else{ window.alert(msg); } },error:function(msg){ window.alert("u6587u4ef6u5220u9664u5931u8d25uff01"); } });5.返回的json绑定到那个参数名上,在form提交时可以获得此值 processCancel.click(function () { if(this_.name) { this_.fileInput.attr("name","delFiles_" + this_.name); } else { this_.fileInput.attr("name","delFiles"); } this_.fileContainer.css("display","none"); this_.delFile(swfUploadInstance); return false; });6.form提交后台处理过程 String[] newFiles = request.getParameterValues(newFileParamName); String[] delFileIDS = request.getParameterValues(delFileParamName);//这个数组是json数组。 处理过程都已写完,希望能帮助到朋友,如何哪里疑问,请给我留言哈 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |