业务背景:实现一个用药人的增加功能,用药人信息中包含附件。如题所示,主要讨论easyui上传的实现。
jsp页面代码(弹出框),一个简单的增加页面
1 div id=addMedicationDlg class=easyui-window closed=true title= iconCls=icon-add style=width350px; height390px;text-aligncenter; background #fafafa; 2 div class=easyui-layout fit=true 3 div region=center border=false style=background#fff;border1px solid #ccc;padding-left 20px; 4 input type=hidden id=mid name=mid 5 form id=addMedicationForm method=POST 6 input type=hidden id=sex 7 table border=0 class=queryTable style=margin-bottom 0px 8 9 tr 10 td class=queryTitle附件:td 11 td class=queryContent 12 input class=inputText type=text id=urlkeywords!-- 放入的是附件路径-- 13 input type=hidden id=goodsNo name=goodsNo !-- 附件列表 -- 14 input type=hidden class=inputText id=swfText 15 span id=keyWordImportBtnspan!--上传按钮-- 16 td 17 td 18 <!--显示进度条--> 19 div id=fsUploadProgressdiv 20 div id=divStatus style=display none;div 21 input id=btnCancel type=buttondisabled=disabled style=display none 22 td 23 tr 24 table 25 table border=0 class=queryTable style=margin-bottom 0px id=fileIdTlb 26 tbody 27 tbody 28 table 29 form 30 div 31 div region=south border=false style=text-aligncenter;height30px;line-height30px; 32 a class=easyui-linkbutton iconCls=icon-ok href=javascriptvoid(0) onclick=doPostData()确定a 33 a class=easyui-linkbutton iconCls=icon-cancel href=javascriptvoid(0) onclick=cancelPostData('addMedicationDlg')取消a 34 div 35 div 36 div
js文件代码
1 /** 2 * 打开修改&增加用药人窗口 3 */ 4 function doModifyMedication (mid){ 5 //弹出修改&增加用药人窗口 6 $('#addMedicationDlg').window({ 7 title '修改用药人', 8 iconCls 'icon-edit', 9 width750, 10 height550, 11 left250, 12 modal true, 13 shadow true, 14 collapsible false, 15 minimizable false, 16 maximizable false 17 }); 18 $(#addMedicationDlg).show(); 19 $('#addMedicationDlg').window('move',{top20}); 20 $('#addMedicationDlg').window('open'); 21 //这一步很关键,初始化上传操作 22 initSWFUpload({ 23 upload_url appPath + pagequeryMedicationkeyWordImport+generateMixed(6), 24 post_params{}, 25 button_placeholder_id keyWordImportBtn, 26 upload_start_handler uploadStartFn, 27 upload_success_handler importOk, 28 file_types .png;.jpg;.gif, 29 debugfalse, 30 custom_settings { 31 progressTarget fsUploadProgress, 32 cancelButtonId btnCancel 33 } 34 }); 35 } 36 /** 37 *保存提交 38 */ 39 function doPostData(){ 40 //序列化表单 41 var userinfo = $('#addMedicationForm').serialize(); 42 doAjax({ 43 urlappPath + 'pagequeryMedicationaddMedication'+ memberId, 44 type'POST', 45 datauserinfo, 46 successfunction(data){ 47 if(data ==true){ 48 $.messager.alert('提示信息','保存成功','info'); 49 $('#selectMedicineManDlg').window('close'); 50 cancelPostData('addMedicationDlg'); 51 lookMedication(memberId); 52 }else{ 53 $.messager.alert('提示信息','抱歉,操作未能完成,'+data,'error'); 54 } 55 }, 56 errorfunction(XMLHttpRequest, textStatus, errorThrown){ 57 $.messager.alert('提示信息','抱歉,操作未能完成,'+textStatus,'error'); 58 } 59 }); 60 } 61 /** 62 *取消提交 63 *@param dlg 64 */ 65 function cancelPostData(dlg){ 66 $('#'+dlg).window('close'); 67 }
后台java代码
1 /** 2 * 保存文件(在文件初始化的时候执行) 3 * 4 * @param request 5 * @param response 6 * @param uploadFile 7 * @throws Exception 8 */ 9 @RequestMapping(value = "/keyWordImport") 10 public Object keyWordImport(HttpServletRequest request, 11 HttpServletResponse response, 12 @RequestParam("uploadFile") MultipartFile f) throws Exception { 13 request.setCharacterEncoding("utf-8"); 14 response.setCharacterEncoding("utf-8"); 15 HashMap<String, Object> map = new HashMap<String, Object>(); 16 String type = "" ; 17 String splitKey = ""; 18 19 //---设置字符集 20 try { 21 request.setCharacterEncoding("utf-8"); 22 } catch (Exception e) { 23 e.printStackTrace(); 24 } 25 response.setCharacterEncoding("utf-8"); 26 27 if (null != request.getParameter("splitKey")){ 28 splitKey = request.getParameter("splitKey"); 29 } 30 31 //---获取文件名称、文件后缀名称以及生成新名称 32 String oldFileName = f.getOriginalFilename(); 33 if(null!=oldFileName && oldFileName.contains(".")){ 34 type = oldFileName.substring(oldFileName.lastIndexOf(".")).toLowerCase(); 35 } 36 String newFileName =oldFileName.substring(0,oldFileName.lastIndexOf("."))+String.valueOf(System.currentTimeMillis())+type; 37 try { 38 // ---获取文件保存目录名称 39 40 String saveFolder = request.getSession().getServletContext() 41 .getRealPath("upload") 42 + "/" + "otcFolder"; 43 44 String newFileNamePath = "/upload/otcFolder/"+ newFileName; 45 map.put("newFileName", newFileNamePath); 46 47 // ---上传 48 Map<String, Object> res = uploadService.doUpload(saveFolder, 49 f.getBytes(), newFileName, splitKey, "otcFolder"); 50 // ---返回值 51 if (0 == (Integer) res.get("error")) { 52 request.setAttribute("res", "success"); 53 response.getWriter().write(org.json.simple.JSONObject.toJSONString(map)); 54 } else { 55 request.setAttribute("res", 56 "err : " + (String) res.get("message")); 57 } 58 } catch (Exception e) { 59 request.setAttribute("res", "err : " + e.getMessage()); 60 logger.error("upload failed in action " + e.getMessage()); 61 } 62 return null; 63 } 64 //保存用药人新增数据 65 @RequestMapping(value="/addMedication/{memberId}") 66 @ResponseBody 67 public String addMedication (HttpServletRequest request ,@PathVariable Long memberId,Medication medication) throws Exception { 68 String fileList = medication.getGoodsNo(); 69 String temps [] =fileList.split(","); 70 List<MedicationMemberFile> list =new ArrayList<MedicationMemberFile>(); 71 for (int i = 0 ;i<temps.length ; i++){ 72 MedicationMemberFile medicationMemberFile = new MedicationMemberFile(); 73 if (temps[i].trim().length() >0 || temps[i].trim() != null){ 74 medicationMemberFile.setFilePath(temps[i]); 75 list.add(medicationMemberFile); 76 } 77 } 78 medication.setMedicationMemberFile(list); 79 medication.setMemberId(memberId); 80 String result =""; 81 String resultDataRecord = SOAJsonClient.sendMsgToOtc(OtcJsonCodeRelation.JsonAssignedDetailsTasksService_setAddMedication,JSON.toJSONString(medication) ); 82 JSONObject objectRecord = JSONObject.fromObject(resultDataRecord); 83 String objectstate=String.valueOf(objectRecord.get("state")); 84 if(objectstate.trim().length() >0 && objectstate.equals("0")){ 85 result ="true"; 86 } 87 return result; 88 }