@RequestMapping(value = "/upload", method = RequestMethod.POST) public void upload(@RequestParam MultipartFile file,@RequestParam(required=false) Integer importFlag, HttpServletRequest request, HttpServletResponse response) throws IOException { // 如果只是上传一个文件,则只需要MultipartFile类型接收文件即可,而且无需显式指定@RequestParam注解 // 如果想上传多个文件,那么这里就要用MultipartFile[]类型来接收文件,并且还要指定@RequestParam注解 // 并且上传多个文件时,前台表单中的所有<input // type="file"/>的name都应该是myfiles,否则参数里的myfiles无法获取到所有上传的文件 int flag = 0; String json; if (file.isEmpty()) { System.out.println("文件未上传"); } else { // 如果用的是Tomcat服务器,则文件会上传到\%TOMCAT_HOME%\webapps\YourWebProject\WEB-INF\upload\文件夹中 String realPath = request.getSession().getServletContext() .getRealPath("/upload"); // 这里不必处理IO流关闭的问题,因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉,我是看它的源码才知道的 File xlsFile = new File(realPath, file.getOriginalFilename()); FileUtils.copyInputStreamToFile(file.getInputStream(), xlsFile); HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream( xlsFile)); HSSFSheet sheet = workbook.getSheetAt(0); List<Salary> salaryList = new ArrayList<Salary>(); Salary salary = null; HSSFRow row = null; for (int i = 2; i < sheet.getLastRowNum() + 1; i++) { row = sheet.getRow(i); salary = new Salary(); //部门编号 salary.setDeptCode(getCellValue(row.getCell(0))); //部门名 salary.setDeptName(getCellValue(row.getCell(1))); //工号 salary.setStaffCode(getCellValue(row.getCell(2))); //姓名 salary.setRealName(getCellValue(row.getCell(3))); //岗资 salary.setPostWage(getCellValue(row.getCell(4))); //薪资 salary.setPay(getCellValue(row.getCell(5))); //绩效 salary.setMeritPay(getCellValue(row.getCell(6))); // 岗位津贴 salary.setTask(getCellValue(row.getCell(7))); // 保留贴 salary.setResponsibility(getCellValue(row.getCell(8))); //课酬 salary.setKeep(getCellValue(row.getCell(9))); //其他 salary.setNet(getCellValue(row.getCell(10))); //补公积 salary.setProvident(getCellValue(row.getCell(11))); // 岗补 salary.setBack(getCellValue(row.getCell(12))); // 课补 salary.setBack2(getCellValue(row.getCell(13))); //临补 salary.setTemporyBack(getCellValue(row.getCell(14))); //应发额 salary.setWages(getCellValue(row.getCell(15))); // 工会 salary.setLabour(getCellValue(row.getCell(16))); //失业保险 salary.setUnemployed(getCellValue(row.getCell(17))); // 医疗保险 salary.setMedical(getCellValue(row.getCell(18))); // 扣公积 salary.setDeductionProvident(getCellValue(row.getCell(19))); // 扣一 salary.setDeductionOne(getCellValue(row.getCell(20))); // 扣 salary.setReserved(getCellValue(row.getCell(21))); // 养老保险 salary.setPension(getCellValue(row.getCell(22))); //税所得 salary.setAfterTaxIncome(getCellValue(row.getCell(23))); // 所得税 salary.setIncomeTax(getCellValue(row.getCell(24))); //Totaldeductio salary.setTotaldeduction(getCellValue(row.getCell(25))); //实发额 salary.setHomepay(getCellValue(row.getCell(26))); salary.setYear(getCellValue(row.getCell(27))); salary.setMonth(getCellValue(row.getCell(28))); salary.setNote(getCellValue(row.getCell(29))); salaryList.add(salary); } if (salaryList.size() > 0) { flag = salaryService.insert(salaryList,importFlag); if (flag > 0) { xlsFile.delete(); } } } if (flag > 0) { json = "{"success":"true","msg":"导入成功!"}"; } else { json = "{"success":"false"}"; } response.setContentType("text/html;charset=utf-8"); try { response.getWriter().write(json); response.getWriter().flush(); response.getWriter().close(); } catch (IOException e) { e.printStackTrace(); } }