异步上传excel带进度条

@Override
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
    FileUploadForm form = (FileUploadForm) command;
    String progressId = "adminId_" + CookieUtil.get(CookieKeyEnum.ADMIN_ID) + "_" + request.getParameter("progressId");
    SimpleResult result = execute(form.getFile(), progressId);
    return new ModelAndView(new JsonView(result));
}

private SimpleResult execute(MultipartFile file, final String progressId) {
    ImptUpsBillsService imptUpsBillsService = ContextFactory.getBean(ImptUpsBillsService.class);
    SimpleResult result = SimpleResult.create(false);
    try {
        InputStream inputStream = file.getInputStream();
        if (inputStream.available() == 0) {
            result.setMessage("文件内无有效数据");
            return result;
        }
        List<List<String>> rows = importCsv(file);
        //根据订单号排序  小到大排序
        List<List<String>> finalRows = rows.stream().sorted(Comparator.comparing(s -> s.get(15))).collect(Collectors.toList());
        new Thread(() -> {
            try {
                imptUpsBillsService.importUpsBills(finalRows, progressId);
                //境内结算
                imptUpsBillsService.orderUsUpsUpdateStatus(finalRows);
            } catch (Exception e) {
                logger.error("failed to orderUpdateStatus", e);
            }
        }).start();
        result.setMessage("后台数据导入进行中...").setSuccess(true);
        return result;
    } catch (Exception e) {
        logger.error("failed to importUpsBills", e);
        result.setMessage("入账操作失败!").setSuccess(false);
        return result;
    }
}

/**
 * 解析csv文件 到一个list中
 * 每个单元个为一个String类型记录,每一行为一个list。
 * 再将所有的行放到一个总list中
 *
 * @return
 * @throws IOException
 */
public static List<List<String>> importCsv(MultipartFile file) {
    List<List<String>> dataList = new ArrayList<>();
    BufferedReader brReader = null;
    InputStreamReader inReader = null;
    try {
        inReader = new InputStreamReader(file.getInputStream());
        brReader = new BufferedReader(inReader);
        String rec = null;//一行
        String str;//一个单元格
        while ((rec = brReader.readLine()) != null) {
            Pattern pCells = Pattern.compile("(\"[^\"]*(\"{2})*[^\"]*\")*[^,]*,");
            Matcher mCells = pCells.matcher(rec);
            List<String> cells = new ArrayList<>(); //每行记录一个list
            //读取每个单元格
            while (mCells.find()) {
                str = mCells.group();
                str = str.replaceAll("(?sm)\"?([^\"]*(\"{2})*[^\"]*)\"?.*,", "$1");
                str = str.replaceAll("(?sm)(\"(\"))", "$2");
                cells.add(str);
            }
            dataList.add(cells);
        }
    } catch (Exception e) {
    } finally {
        if (brReader != null) {
            try {
                brReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (inReader != null) {
            try {
                inReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return dataList;
}

 

posted @ 2017-11-14 16:33  令狐る侠  阅读(1063)  评论(0编辑  收藏  举报