经过struts的FormFile上传单个excel文件html
思路:java
一、经过struts的FormFile获取File(这个文件的路径是“客户端的选择的路径地址”)apache
二、将客户端的文件,以流的形式,存放到服务器端指定的目录服务器
三、读取服务器端的excel文件,先获取工做簿workbook,而后获取这个工做簿的sheet,而后获取sheet中的每一行app
四、校验: (1)、将sheet中的数据,按行读取,并插入到临时表中less
(2)、针对此次导入数据,按列校验——每一列一个select,查出不符合规则的idjsp
(3)、若是校验不经过,针对同一个错误,返回一批id,经过request返回到页面this
(4)、若是校验都经过,将临时表中的数据拷贝到正式表,清空临时表,将正式表的数据封装成对象返回spa
jsp页面: <td colspan="2"> <html:file property="theFile" size="10" /> </td> <td> <a onClick="if(document.all('theFile').value!='') uploadAndCheck();"class="butlink"> <span class="but2"> 上传 </span> </a> </td> function uploadAndCheck(){ orgStaffChangeForm.action=orgStaffChangeForm.action; setMethodAndNoConfirm('uploadAndCheck'); } 后台方法: [java] view plaincopy public ActionForward uploadAndCheck(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception { OrgStaffChangeForm frm = (OrgStaffChangeForm)form; //经过struts的FormFile获取文件 FormFile file = (FormFile)frm.getTheFile(); String fileName = (String)file.getFileName(); int fileSize = file.getFileSize(); //经过getInputStream()方法取得文件流 BufferedInputStream bis = null; BufferedOutputStream bos = null; InputStream is = null; OutputStream fos = null; String filepath=null; try { //此处若没有upload目录,则新建此目录 java.io.File dir = new java.io.File(this.getServlet().getServletContext().getRealPath("/")+"upload//"); if(!dir.exists()){ dir.mkdir(); } is = (InputStream) file.getInputStream();//把文件读入 bis = new BufferedInputStream(is); filepath = this.getServlet().getServletContext().getRealPath("/")+"upload//"+fileName;//取当前系统路径 fos = new FileOutputStream(filepath);//创建一个上传文件的输出流 bos = new BufferedOutputStream(fos); //文件最大限额 int fileMaxSize = 10 * 1024 * 1024; if (fileSize > fileMaxSize) { //文件大小不能超过fileMaxSize,若是超过,报"上传文件尺寸超过10M"错误; throw new Exception("上传文件大小不能超过10M"); } int bytesRead = 0; byte[] buffer = new byte[5 * 1024]; while ((bytesRead = bis.read(buffer)) != -1) { //将文件从客户端读入到服务器指定目录 bos.write(buffer, 0, bytesRead); } }catch (Exception e) { //设置文件物理上传出现问题时的出现的错误信息 throw new Exception("上传文件时发生错误,请重试或与管理员联系!"); }finally { if (bos != null) { bos.close(); } if (bis != null) { bis.close(); } } Workbook workbook = Workbook.getWorkbook(new File(filepath)); Sheet sheet = workbook.getSheet(0); int rowCount = sheet.getRows(); Map result = new HashMap(); if(rowCount < 21){ result.put("less", "导入数据不能小于20行!"); request.setAttribute("result", result); return mapping.findForward(MAIN_FORM); }else if(rowCount > 2000){ throw new Exception("导入数据不能大于2000行!"); } List list = new ArrayList(); for (int k = 1;k<rowCount;k++){ Cell[] cellDetail = sheet.getRow(k);//经过cellDetail[i]可得到每个单元格的数据 list.add(cellDetail);//list中存放的是excel中一行的数据 } //将数据传到后台处理 IOrgStaffChangeManager boam = (IOrgStaffChangeManager) getBean("orgStaffChangeManager"); ArrayList resultList = this.organizeCheckData(frm, request, response); result = boam.dealUploadData(list,frm.getUploadId(),resultList); frm.setEmpChangeUpload(true); request.setAttribute("result", result); //数据校验经过 if( result.get("success") !=null){ List listempUpload = new ArrayList();// 校验经过后,存放返回的对象的list List collSalaryLists=(List)result.get("success"); HashSet orgEmpChanges = new HashSet(); Iterator iter=collSalaryLists.iterator(); while (iter.hasNext()){ OrgEmpChangeTemp cell = (OrgEmpChangeTemp) iter.next(); OrgEmpChange orgEmpChange = new OrgEmpChange(); orgEmpChange.setNewEmpId(cell.getNewEmpId()); ....... listempUpload.add(orgEmpChange); } } frm.setUploadId(frm.getUploadId()); request.setAttribute("wls", wls); request.setAttribute("listempUpload", listempUpload); return mapping.findForward(MAIN_FORM); }
注意事项:excel
一、FormFile是struts包对外的一个接口,并且org.apache.struts.upload包是使用的commons-fileupload-1.0进行的封装。
FormFile的实现依然使用commons-fileupload-1.0版本的DiskFileUpload类。
DiskFileUpload这个类,commons-fileupload已经弃用了,取而代之的是ServletFileUpload类了
二、若是使用了它来实现文件上传的功能,则必须是FormFile对象在被初始化之后才能使用,它在进入Action以前就已经初始化好了!
三、struts是默认使用org.apache.struts.upload.CommonsMultipartRequestHandler类来处理FormFile指定的上传文件的。
四、 Struts根本没有把上传过程当中出的超出最大值的异常带到Action,由于那是不可能的,而是把它放到了rquest的Attribute里。
而出了其余异常如enctype不对,磁盘空间不足怎么办?很遗憾,Struts没有去处理它,而是log了一下,抛给了上一层了。