一.文件上传 easyUI+ajax+SpringMVC
上传两个指定格式文件及基本信息,入库基本信息+将文件保存到服务器
1.jsp页面
<div id="templatedlg" class="easyui-dialog" style="width:400px" closed="true" buttons="#templatedlg-buttons" data-options="modal:true"> <form id="templatefm" method="post" enctype="multipart/form-data" style="width:360px"> <div style="margin-bottom:20px;font-size:14px;border-bottom:1px solid #ccc">模板信息</div> <div style="margin-bottom:10px"> <input id="id" name="id" type="hidden"> <input id="sourceCode" name="tplCode" class="easyui-textbox" required="true" label="编码:" style="width:100%"> </div> <div style="margin-bottom:10px"> <input id="sourceName" name="tplName" class="easyui-textbox" required="true" label="名称:" style="width:100%"> </div> <div style="margin-bottom:10px"> <input id="templateFile" class="easyui-filebox" name="templateFile" label="模板文件:" data-options="prompt:'请选择文件...'" style="width:100%"> </div> <div style="margin-bottom:10px"> <input id="jsonFile" class="easyui-filebox" name="jsonFile" label="json文件:" data-options="prompt:'请选择文件...',accept:'application/json'" style="width:100%"> </div> </form> </div> <div id="templatedlg-buttons"> <a id="templatesave" href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" style="width:90px">保存</a> <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#templatedlg').dialog('close')" style="width:90px">取消</a> </div> <div id="templateloading"> <div class="inputdiv" > <img class="prtspan span_1" src="${ctx}/statics/images/loading.gif"/> <h3 id="templatelodingMsg" class="prtspan span_1" >文件正在上传,请稍候......</h3> </div> </div>
2.js代码
//(新增、编辑)弹窗保存 $('#templatesave').bind("click", function(){ var isValid = $("#templatefm").form('validate'); if(!isValid){ return isValid; } $('#templatefm').form({ url: ctx + templateSaveOrUpdateURL, onSubmit: function () { template_loading_generate("upload"); }, success: function (date) { $('#templateloading').window('close'); var result = JSON.parse(date); if (result.code == 0){ msgAlert('文件上传成功','success'); $('#templatedlg').dialog('close'); // close the dialog $('#templatedg').datagrid('reload'); // reload the role data } else { showMsg(result, 'error'); } }, error: function (date) { $('#templateloading').window('close'); msgAlert('文件上传失败,请重试','error'); } }); $('#templatefm').submit(); }); //生成上传提示框 function template_loading_generate(type){ $('#templateloading').window({ title:'系统提示', width:'300px', closable:false, collapsible:false, minimizable:false, maximizable:false, resizable:false, modal:true }); $("#templatelodingMsg").text("文件正在上传,请稍候......") $('#templateloading').window('open'); }
3.controller
/** * 新增 */ @ResponseBody @RequestMapping(value = "/save", method = RequestMethod.POST) public R saveDataSource( @RequestParam String tplCode, @RequestParam String tplName, @RequestParam MultipartFile templateFile, @RequestParam MultipartFile jsonFile, HttpServletRequest request ) { Subject subject = SecurityUtils.getSubject(); Object principal = subject.getPrincipal(); JSONObject jsonObject = JSONObject.fromObject(principal); String userId = jsonObject.getString("id"); String tempName = templateFile.getOriginalFilename(); String jsonName = jsonFile.getOriginalFilename(); String[] tempType = tempName.split("\\."); String[] jsonType = jsonName.split("\\."); String check = fileCheck(tempType, jsonType); if(!StringUtils.isEmpty(check)){ return R.error(check); } ReptTemplateVO template = new ReptTemplateVO(); template.setTplCode(tplCode); template.setTplName(tplName); template.setJsonFile(jsonFile); template.setTemplateFile(templateFile); template.setTplFile(tempType[0]); template.setCreaterId(Long.valueOf(userId)); template.setCreateTime(DateUtils.getCurrentTime()); templateService.upload(template,request,0); return R.ok(); }
4.service
@Override public int upload(ReptTemplateVO template, HttpServletRequest request, Integer flag) { MultipartFile jsonFile = template.getJsonFile(); MultipartFile templateFile = template.getTemplateFile(); String tplFile = template.getTplCode(); // 保存文件到服务器 String realPath = request.getSession().getServletContext().getRealPath("/WEB-INF/jasper"); try { if (!jsonFile.isEmpty()) SaveFileFromInputStream(jsonFile, realPath , tplFile + ".json"); if (!templateFile.isEmpty()) SaveFileFromInputStream(templateFile, realPath , tplFile + ".jasper"); } catch (IOException e) { e.printStackTrace(); } // 入库记录 if (flag == 0) templateDao.save(template); if (flag == 1) { templateDao.update(template); } return 0; } // 保存文件 public void SaveFileFromInputStream(MultipartFile filedata, String path, String savefile) throws IOException { File tempFile = new File(path); if (!tempFile.exists()) { tempFile.mkdirs(); } // 保存文件到服务器 FileOutputStream fos; try { fos = new FileOutputStream(path + "//" + savefile); fos.write(filedata.getBytes()); fos.close(); // finalTime=(int)System.currentTimeMillis(); } catch (Exception e) { e.printStackTrace(); } }