SWFUpload综合运用。

5 篇文章 0 订阅


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_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("\u6587\u4ef6\u5220\u9664\u5931\u8d25\uff01");
			}
		});
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数组。

处理过程都已写完,希望能帮助到朋友,如何哪里疑问,请给我留言哈 大笑


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值