问题描述
我是直接采用属性封装:Action中定义各个的属性要与对应form中的参数同名,并提供GET/SET方法;这是jsp页面的代码<!--导入数据面板--><divid="dlg2"class="easyui-dialog"style="width:400px;height:180px;padding:10px20px"closed="true"buttons="#dlg-buttons2"><formid="uploadForm"action="<%=contextPath%>/base/cmorganization!export.gx"method="post"enctype="multipart/form-data"><table><tr><td>下载模版:</td><td><ahref="javascript:void(0)"class="easyui-linkbutton"onclick="downloadTemplate()">导入模版</a></td></tr><tr><td>上传文件:</td><td><inputtype="text"name=cmorganizationUploadFile></td><!--<td><inputtype="text"name=cmorganizationUploadFile></td>--></tr></table></form></div><divid="dlg-buttons2"><ahref="javascript:void(0)"class="easyui-linkbutton"iconCls="icon-ok"onclick="uploadFile()">上传</a><ahref="javascript:void(0)"class="easyui-linkbutton"iconCls="icon-cancel"onclick="javascript:$('#dlg2').dialog('close')">关闭</a></div>
这是提交方法//上传varuploadFile=function(){$.post(gx.contextPath+'/base/cmorganization!upload.gx',gx.serializeObject($('uploadForm')),function(result){if(result.success){varresult=eval('('+result+')');if(result.errorMsg){$.messager.alert("系统提示",result.errorMsg);}else{$.messager.alert("系统提示","上传成功");$("#dlg2").dialog("close");$("#dg").datagrid("reload");}}},'json');}
这是action中的代码privateFilecmorganizationUploadFile;//导入文件publicFilegetCmorganizationUploadFile(){returncmorganizationUploadFile;}publicvoidsetCmorganizationUploadFile(FilecmorganizationUploadFile){this.cmorganizationUploadFile=cmorganizationUploadFile;}publicStringupload()throwsException{System.out.println("进入action了!");System.out.println("=============="+cmorganizationUploadFile);/*POIFSFileSystemfs=newPOIFSFileSystem(newFileInputStream(cmorganizationUploadFile));HSSFWorkbookwb=newHSSFWorkbook(fs);HSSFSheethssfSheet=wb.getSheetAt(0);if(hssfSheet!=null){for(introwNum=1;rowNum<=hssfSheet.getLastRowNum();rowNum++){HSSFRowhssfRow=hssfSheet.getRow(rowNum);if(hssfRow==null){continue;}System.out.println(ExcelUtil.formatCell(hssfRow.getCell(0)));TCrCmOrganizationcmOrganization=newTCrCmOrganization();cmOrganization.setCmName(ExcelUtil.formatCell(hssfRow.getCell(0)));cmOrganization.setCmTimeks(DateUtil.stringToDate(ExcelUtil.formatCell(hssfRow.getCell(1))));cmOrganization.setCmTimejs(DateUtil.stringToDate(ExcelUtil.formatCell(hssfRow.getCell(2))));cmOrganization.setCmOrganizer(ExcelUtil.formatCell(hssfRow.getCell(3)));cmOrganization.setCmCapacity(ExcelUtil.formatCell(hssfRow.getCell(4)));cmOrganization.setCmContent(ExcelUtil.formatCell(hssfRow.getCell(5)));cmOrganization.setCmHost(ExcelUtil.formatCell(hssfRow.getCell(6)));cmOrganization.setCmRecordpeople(ExcelUtil.formatCell(hssfRow.getCell(7)));cmOrganization.setCmSummaryLevel(ExcelUtil.formatCell(hssfRow.getCell(8)));service.save(cmOrganization);}}*/JSONObjectresult=newJSONObject();result.put("success","true");ResponseUtil.write(ServletActionContext.getResponse(),result);returnnull;}
后台打印结果进入action了!==============null
解决方案
解决方案二:
privateStringcmorganizationUploadFile;//导入文件publicStringgetCmorganizationUploadFile(){returncmorganizationUploadFile;}publicvoidsetCmorganizationUploadFile(StringcmorganizationUploadFile){this.cmorganizationUploadFile=cmorganizationUploadFile;}
这个
解决方案三:
文件上传不用form的话,就得借助插件了,你这种写法是不行的,serializeObject应该没法序列化file。
解决方案四:
你的form表单里加了这个enctype="multipart/form-data",出了type=file类型的可以直接在Action里面接收,其他的好像要判断一下的,而且上传文件不是应该是<inputtype="file"/>吗?你这里怎么是个type="text'
解决方案五:
<inputtype="file"enctype="multipart/form-data">
解决方案六:
本来是这样的<td><inputtype="file"name=cmorganizationUploadFile></td>
但是传过去报空指针异常,所以我想是不是file不能用,就换成了text,但还是null.