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;
    }
  • 相关阅读:
    file is universal (3 slices) but does not contain a(n) armv7s slice error for static libraries on iOS
    WebImageButton does not change images after being enabled in Javascript
    ajax OPTION
    编程遍历页面上所有TextBox控件并给它赋值为string.Empty?
    获取海洋天气预报
    C#线程系列教程(1):BeginInvoke和EndInvoke方法
    js控制只能输入数字和小数点
    Response.AddHeader(,)
    ManualResetEvent的理解
    Convert.ToInt32、int.Parse(Int32.Parse)、int.TryParse、(int) 区别
  • 原文地址:https://www.cnblogs.com/dreammyone/p/7833254.html
Copyright © 2011-2022 走看看