SpringMVC中文件上传,添加enctype=”multipart/form-data”后表单其他属性为空
问题
最近在开发一个银行的管理系统,用的是SpringMVC+Spring+Mybatis框架,在表单中上传文件的时候出现了form提交数据后,后台接收不到文件为null的情况我的form表单如下所示:
<form action="asset/projectApplySave.do" name="addProjectMForm" id="addProjectMForm" method="post" enctype="multipart/form-data"> <div id="zhongxin" style="margin-left: 10px;margin-right: 10px;margin-top: 15px;"> <!-- 隐藏传送审批ID --> <input type="hidden" id="apply_procedure_id" name="apply_procedure_id"> <table id="table_report" class="table text-table table-striped table-bordered table-hover"> <tr> <td style="text-align: center;" colspan="6"><label><b style="color: red"><h2>新增项目</h2></b></label></td> </tr> <tr> <td><label>项目名称<b style="color: red">*</b></label></td> <td><input type="text" name="apply_name" id="apply_name" placeholder="请输入项目名称"></td> <td><label>设备名称<b style="color: red">*</b></label></td> <td><select class="chzn-select" name="device_name" id="device_name" data-placeholder="设备名称-供应商" style="width: 221px; vertical-align: center;"> <option></option> <c:forEach items="${product_find}" var="pf" varStatus="vs"> <option value="${pf.product_code}@${pf.product_class}@${pf.product_name}@${pf.provider_name}">${pf.product_name}-${pf.provider_name}</option> </c:forEach> </select></td> <td><label>申请时间<b style="color: red">*</b></label></td> <td><input name="apply_time_show" id="apply_time_show" class="span10 date-picker" type="text" disabled="disabled" value="${apply_time }" data-date-format="yyyy-mm-dd" style="width: 208px;" placeholder="申请日期" title="申请日期" /></td> <!-- 隐藏元素 用于传送申请时间 因为readonly的情况下 还能进行选择 disable的情况下 不能进行值传递 只能使用隐藏元素传递值的方法--> <input type="text" name="apply_time" id="apply_time" style="display: none;"/> </tr> <tr> <td><label>申请公司<b style="color: red">*</b></label></td> <td style="vertical-align: top;"><select class="chzn-select" data-placeholder="请选择申请公司" name="apply_company" id="apply_company" onchange="select_company();"> <option></option> </select></td> <!-- class="chzn-select" --> <td><label>申请部门<b style="color: red">*</b></label></td> <td style="vertical-align: top;"><select class="chzn-select" name="apply_dept" id="apply_dept" data-placeholder="请选择下级机构" style="width: 221px; vertical-align: center;"> <option></option> </select></td> <td><label>申请人<b style="color: red">*</b></label></td> <td><input type="text" name="apply_person" id="apply_person" placeholder="申请人"></td> </tr> <tr> <td><label>型号<b style="color: red">*</b></label></td> <td><input type="text" name="device_model" id="device_model" placeholder="型号"></td> <td><label>市场价格(元)<b style="color: red">*</b></label></td> <td><input type="text" name="device_price" id="device_price" placeholder="资产预单价"></td> <td><label>数量(个)<b style="color: red">*</b></label></td> <td><input type="text" name="device_number" id="device_number" placeholder="资产数量"></td> </tr> <tr> <td><label>设备用途<b style="color: red">*</b></label></td> <td><input type="text" name="device_purpose" id="device_purpose" placeholder="设备用途"></td> <td><label>预计使用年限(年)<b style="color: red">*</b></label></td> <td><input type="text" name="device_use_years" id="device_use_years" placeholder="预计使用年限"></td> <td><label>审批流程<b style="color: red">*</b></label></td> <td style="vertical-align: top;"><select class="chzn-select" name="apply_procedure" id="apply_procedure" data-placeholder="请选择审批流程" style="width: 221px; vertical-align: center"> <option></option> <c:forEach items="${approvalProcess }" var="process"> <option id=${process.process_Id }>${process.process_Name}</option> </c:forEach> </select></td> </tr> <tr> <td><label>上传申请报告 </label></td> <td style="vertical-align: middle;"><input type="file" name="address" id="apply_report_address" placeholder="上传申请报告" style="width: 221px"></td> <td><label>申请备注</label></td> <td colspan="3"><textarea rows="2" cols="" name="apply_remarks" id="apply_remarks" style="width: 91%"> </textarea></td> </tr> <tr> <td><label>申请原因</label></td> <td colspan="6"><textarea rows="2" cols="" name="apply_reason" id="apply_reason" style="width: 95%"> </textarea></td> </tr> <tr style="height: 40"> <td style="text-align: center;" colspan="6"><a class="btn btn-danger" onclick="save();">提交申请</a> <a class="btn btn-primary" href="asset/atp_showForm.do">取消</a> </td> </tr> </table> </div> <div id="zhongxin2" class="center" style="display: none"> <br /> <br /> <br /> <br /> <img src="static/images/jiazai.gif" /><br /> <h4 class="lighter block green"></h4> </div> </form>
解决办法
后来在网上查资料,发现是”enctype=”multipart/form-data”的问题。删掉之后form表单数据可以被接收到,但是文件上传失败,不删的话文件上传成功,表单数据接收不到。试了很多种方法,都没有成功,以前是通过自己封装的一个工具类来获取jsp页面的数据的,但是显然这种方法已经不在适合上传文件的时候在后台接收数据,所以我改用了实体类来接收页面传过来的数据。代码如下:
/** * 项目立项增加界面提交 * @param file * @param projectApply * @return * @throws Exception */ @RequestMapping(value = "/projectApplySave") public String projectApplySave(@RequestParam(value = "address", required = false) MultipartFile file,ProjectApply projectApply) throws Exception { ModelAndView mv = this.getModelAndView(); //通过session获取登录用户名 Subject currentUser = SecurityUtils.getSubject(); Session session = currentUser.getSession(); User user = (User) session.getAttribute(Const.SESSION_USER); String user_name = user.getUSERNAME(); String filePath = ""; if (!file.getOriginalFilename().equals("")) { long time = System.currentTimeMillis(); int number = (int)(Math.random()*900)+100; String name = time+""+number+""+user_name+"@"+file.getOriginalFilename(); filePath = PathUtil.getClasspath() + Const.FILEPATHFILE + name; // 文件上传路径 FileCopyUtils.copy(file.getInputStream(), new FileOutputStream(filePath)); } projectApply.setApply_report_address(filePath); projectApplyService.saveProjectApply(projectApply); return "redirect:/asset/atp_showForm?saveResult=success"; }
这样就顺利的form表单的数据插入到了数据库,文件上传到了服务器。