經過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了一下,拋給了上一層了。