zoukankan      html  css  js  c++  java
  • 异步上传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;
    }
  • 相关阅读:
    模拟Linux修改实际、有效和保存设置标识
    ubuntu中桌面图标的配置
    硬盘安装ubuntu
    关于NumPy
    关于Spring JavaWeb工程中的ContextRefreshedEvent事件
    MySQL中Index Merge简介
    InetlliJ IDEA的快捷键及各种配置
    Java语言中的正则表达式
    Git使用笔记
    linux中添加常用应用程序的桌面图标
  • 原文地址:https://www.cnblogs.com/dreammyone/p/7833254.html
Copyright © 2011-2022 走看看