freemaker 部分 <form action="${base}/lot/save" method="post" enctype="multipart/form-data" > <input type="file" name="file" class="button input-file" id="file" /> <div class="form-button"><br/> <button id="submit_edit2" class="button bg-main">批量导入</button> </div> </form> <br/> <h1>导入信息反馈 :</h1> <br/> <#list mess! as x> ${x!} <br/> </#list>
@RequestMapping(value = { "/leadin/lot/save" }) public String lotSave(@RequestParam(value = "file", required = false) MultipartFile file, RedirectAttributesModelMap model, HttpSession httpsession, HttpServletRequest request) { List<String> list = new ArrayList<String>(); YellowpagesitemDO yellowpagesitemDO = null; String path = request.getSession().getServletContext().getRealPath("upload"); // String fileName = file.getOriginalFilename(); String fileName = new Date().getTime() + ".csv"; File targetFile = new File(path, fileName); if (!targetFile.exists()) { targetFile.mkdirs(); } try { file.transferTo(targetFile); } catch (Exception e) { e.printStackTrace(); } // 解析上传的 xlsx 文件 并导入 mysql 数据库中 // 解析 csv 格式文件并保存到mysql 数据库 String filePath = path + "\\" + fileName; System.out.println(filePath); BufferedReader bufferedReader = null; try { bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath))); String line = null; int j=0; try { while ((line = bufferedReader.readLine()) != null) { j=j+1; String[] columns = line.split(","); String t=""; if(columns.length<6){ for(int i=0;i<columns.length;i++){ t= t+columns[i]+","; } list.add(t + " 格式不对或不能含有换行符,导入失败!"); }else{ String shen = columns[0]; //获取 shen yellowpagesitemDO = new YellowpagesitemDO(); yellowpagesitemDO.setShen(shen); YellowpagesitemDO yCourt = yellowPage01Service.getCourt(yellowpagesitemDO); if (yCourt == null) { // 法院不存在 查区域id YellowpagesitemDO yAreaId = yellowPage01Service.getAreaId(yellowpagesitemDO); String areaId = yAreaId.getAreaId(); yellowpagesitemDO.setAreaId(areaId); System.out.println("+++"+areaId); // 根据区域id 插入法院 OperatorDO opDo = (OperatorDO) httpsession.getAttribute(Constant.SESSION_USER_KEY);// 获取当前用户 yellowpagesitemDO.setUpdUser(opDo.getName());// 获取并且设置当前用户name yellowPage01Service.insertCourt(yellowpagesitemDO); System.out.println(yellowpagesitemDO.getCourt() + " 法院插入成功"); // 再查法院 获取 它的 id 为 courtId yCourt = yellowPage01Service.getCourt(yellowpagesitemDO); } int courtId = yCourt.getCourtId(); System.out.println("法院id已找到, 准备插入 项目名称 和值" + courtId); yellowpagesitemDO.setCourtId(courtId); OperatorDO opDo = (OperatorDO) httpsession.getAttribute(Constant.SESSION_USER_KEY);// 获取当前用户 yellowpagesitemDO.setUserId(Integer.parseInt(opDo.getRoleId()));// 获取并且设置当前用户id yellowPage01Service.insertItem(yellowpagesitemDO); System.out.println(shen + "," + shi + "," + qu + "," + court + "," + name + "," + value + "导入成功"); list.add(shen + "," + shi + "," + qu + "," + court + "," + name + "," + value + " 导入成功"); }} } catch (IOException e) { } } catch (FileNotFoundException e) { e.printStackTrace(); } finally { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } model.addFlashAttribute("mess", list); model.addFlashAttribute(Constant.MESSAGE, " 操作成功。"); return "redirect:"; }