コード例 #1
0
 /**
  * 从excel文件导入客户数据到本地数据库
  *
  * @return
  */
 @RequestMapping(params = "method=importOrderFromExcel")
 @ResponseBody
 public void importOrderFromExcel(HttpServletRequest request, HttpServletResponse response) {
   ImportContext importContext = new ImportContext();
   String result = null;
   importContext.setShopId((Long) request.getSession().getAttribute("shopId"));
   String importRecordIds = request.getParameter("importRecordId");
   List<Long> importRecordIdlist = NumberUtil.parseLongValues(importRecordIds);
   Map<String, String> fieldMapping =
       JsonUtil.jsonToStringMap(request.getParameter("fieldMapping"));
   if (importRecordIdlist == null
       || importRecordIdlist.isEmpty()
       || fieldMapping == null
       || fieldMapping.isEmpty()) {
     result = ImportConstants.MESSAGE_NEED_FILE_OR_MAPPING;
   }
   importContext.setImportRecordIdList(importRecordIdlist);
   importContext.setFieldMapping(fieldMapping);
   importContext.setType(ImportConstants.Type.TYPE_ORDER);
   ImportResult importResult = null;
   try {
     importResult = txnService.importOrderFromExcel(importContext);
   } catch (Exception e) {
     LOG.error(e.getMessage(), e);
   }
   result = JsonUtil.objectToJson(importResult);
   PrintWriter writer = null;
   try {
     writer = response.getWriter();
     writer.write(result);
     writer.flush();
     writer.close();
   } catch (IOException e) {
     LOG.error("/importOrder.do");
     LOG.error("method=importOrderFromExcel");
     LOG.error("单据导入出错");
     LOG.error(e.getMessage(), e);
   } catch (Exception e) {
     LOG.error("/importOrder.do");
     LOG.error("method=importOrderFromExcel");
     LOG.error("单据导入出错");
     LOG.error(e.getMessage(), e);
   }
 }
コード例 #2
0
  /**
   * 导入前: 1.读取excel文件表头 2.保存导入记录(文件)到数据库
   *
   * @param request
   * @return
   */
  @RequestMapping(params = "method=uploadExcel")
  public void getExcelHeader(HttpServletRequest request, HttpServletResponse response) {
    IImportService importService = ServiceManager.getService(IImportService.class);
    IPrivilegeService privilegeService = ServiceManager.getService(IPrivilegeService.class);
    Map result = new HashMap();
    try {
      MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
      MultipartFile multipartFile = multipartRequest.getFile("selectfile");
      String fileName = multipartFile.getOriginalFilename();
      if (!validateOrderType(fileName)) { // todo 不起作用
        response.setHeader("Charset", "UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = response.getWriter();
        result.put("orderImportMsg", "上传文件名无法识别,请使用标准模板名!");
        String resultStr = JsonUtil.mapToJson(result);
        writer.write(resultStr);
        writer.flush();
        writer.close();
        return;
      }
      InputStream inputStream = multipartFile.getInputStream();
      if (inputStream.available() > ImportConstants.UPLOAD_FILE_MAX_SIZE) {
        PrintWriter writer = response.getWriter();
        writer.write(
            "上传文件大小不能超过" + NumberUtil.byteToMillion(ImportConstants.UPLOAD_FILE_MAX_SIZE) + "M!");
        writer.flush();
        writer.close();
        return;
      }
      byte[] fileContent = IOUtil.readFromStream(inputStream);
      // 先读取表头
      ImportContext importContext = new ImportContext();
      importContext.setFileContent(fileContent);
      importContext.setFileName(fileName);
      List<String> headList = importService.parseHead(importContext);
      if (headList == null || headList.isEmpty()) {
        result.put("message", "解析导入文件头部失败!");
      } else {
        result.put("headList", headList);
      }

      // 再存入数据库
      ImportRecordDTO importRecordDTO = new ImportRecordDTO();
      importRecordDTO.setShopId(WebUtil.getShopId(request));
      importRecordDTO.setStatus(ImportConstants.Status.STATUS_WAITING);
      importRecordDTO.setType(ImportConstants.Type.TYPE_ORDER);
      importRecordDTO.setFileName(fileName);
      importRecordDTO.setFileContent(fileContent);
      importRecordDTO = importService.createImportRecord(importRecordDTO);
      if (importRecordDTO == null || importRecordDTO.getId() == null) {
        result.put("message", "保存导入记录出错!");
      } else {
        result.put("importRecordId", String.valueOf(importRecordDTO.getId()));
      }
      result.put(
          "systemFieldList",
          getOrderFieldList(
              PrivilegeRequestProxy.verifierShopVersionResourceProxy(
                  request, LogicResource.WEB_VERSION_WHOLESALERS)));
      String resultStr = JsonUtil.mapToJson(result);
      response.setHeader("Charset", "UTF-8");
      response.setContentType("text/html;charset=UTF-8");
      PrintWriter writer = response.getWriter();
      writer.write(resultStr);
      writer.flush();
      writer.close();
    } catch (Exception e) {
      LOG.error("上传文件出现异常!");
      LOG.error(e.getMessage(), e);
    }
  }