java,Email解析XML,并反写内容

发送邮件含Excel,表头数据,记录如下,异步查询提高查询效率:

  @RequestMapping("email")
    public void email(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //加载邮件html模板long startTime =
        String fileName = "scale-alarm.html";
        URL url = this.getClass().getClassLoader().getResource("templates/" + fileName);
        StringBuffer buffer = new StringBuffer();
        BufferedInputStream bis = null;
        try {
            File f = new File(url.toURI());
            FileInputStream fis = new FileInputStream(f);
            bis = new BufferedInputStream(fis);
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            String s = "";
            while ((s = br.readLine()) != null) {
                buffer.append(s).append("\n");
            }
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    System.out.println(e);
                }
            }
        }
        //查询接口数据
        //存储一个公共的缓存用于拿到总体值
        PlaceHolder ph = new PlaceHolder();
        redis.setEx(ApiConstant.EMAIL_DAY.join("_", "total"), JSONObject.toJSONString(ph), 15L, TimeUnit.HOURS);
        //查询表头数据
        long startTime = System.currentTimeMillis();
        CompletableFuture<String> f1 = CompletableFuture.supplyAsync(() -> {
            try {
                //查询Excel,之后查询表头
                String sdate = null;
                String edate = null;
                emailTitleData.getYdgcsxdje(sdate, edate);
            } catch (Exception e) {
                logger.error("runTitleNum->Exception:{}", e);
            }
            return "";
        });
 
        CompletableFuture<String> f2 = CompletableFuture.supplyAsync(() -> {
            try {
                //查询Excel,之后查询表头
                try {
                    homePageService.queryDaySalesYear(DateUtils.getYearDay(), DateUtils.getYearLast());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                logger.error("runTitleNum->Exception:{}", e);
            }
            return "";
        });
        String headMsg = EmailTemplate.headMsg;
        CompletableFuture.allOf(f1, f2).join();
        String ph1 = redis.get(ApiConstant.EMAIL_DAY.join("_", "total"));
        PlaceHolder phNew = JSONObject.parseObject(ph1, PlaceHolder.class);
        Object[] headParams = {//自己的参数};
        String headMsgNew = MessageFormat.format(headMsg, headParams);
        //查询Excel,之后查询表头
        long futureSqTime = System.currentTimeMillis();
        CompletableFuture<List<DataTemplate>> futureSq = CompletableFuture.supplyAsync(() -> {
            try {
                return emailExcelData.getSqExcel();
            } catch (Exception e) {
                logger.error("getSqExcel->Exception:", e);
            }
            return null;
        });
        CompletableFuture<List<DataTemplate>> futureZr = CompletableFuture.supplyAsync(() -> {
            try {
                return emailExcelData.getZrSqtpExcel();
            } catch (Exception e) {
                logger.error("getZrSqtpExcel->Exception:", e);
            }
            return null;
        });
        CompletableFuture<List<DataTemplate>> futureYd = CompletableFuture.supplyAsync(() -> {
            try {
                return emailExcelData.getYdExcel();
            } catch (Exception e) {
                logger.error("getYdExcel->Exception:", e);
            }
            return null;
        });
        CompletableFuture.allOf(futureSq, futureZr, futureYd).join();
        System.out.println("接口EmailController耗时futureSq:"+String.valueOf(System.currentTimeMillis()-futureSqTime));
        long statrTimeTd = System.currentTimeMillis();

        String headerzrsq = "<td>列1</td><td>列2</td><td>列3</td><td>列4</td><td>列5</td><td>列6</td><td>列7</td><td>列8</td>";
        StringBuilder linesBufferzrsq = new StringBuilder();
        try {
            List<DataTemplate> listSq = futureZr.get();
            listSq.forEach(dataFactory -> {
                linesBufferzrsq.append("<tr><td>" + dataFactory.getStr1() +
                        "</td><td>" + dataFactory.getStr2() +
                        "</td><td>" + dataFactory.getStr3() +
                        "</td><td>" + dataFactory.getStr4() +
                        "</td><td>" + dataFactory.getStr5() +
                        "</td><td>" + dataFactory.getStr6() +
                        "</td><td>" + dataFactory.getStr7() +
                        "</td><td>" + dataFactory.getStr8() + "</td></tr>");
            });
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        String header = "<td>列1</td><td>列2</td><td>列3</td><td>列4</td><td>列5</td><td>列6</td><td>列7</td><td>列8</td>";
        StringBuilder linesBuffer = new StringBuilder();
        List<DataTemplate> list3 = null;
        try {
            list3 = futureYd.get();
            list3.forEach(dataFactory -> {
                linesBuffer.append("<tr><td>" + dataFactory.getStr1() +
                        "</td><td>" + dataFactory.getStr2() +
                        "</td><td>" + dataFactory.getStr3() +
                        "</td><td>" + dataFactory.getStr4() +
                        "</td><td>" + dataFactory.getStr5() +
                        "</td><td>" + dataFactory.getStr6() +
                        "</td><td>" + dataFactory.getStr7() +
                        "</td><td>" + dataFactory.getStr8() + "</td></tr>");
            });
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        String emailHeadColor = "#FF0000";//颜色
        Object[] params = {//自己参数      };
        //填充html模板中的五个参数
        String htmlText = MessageFormat.format(buffer.toString(), params);
        //改变表格样式
        htmlText = htmlText.replaceAll("<td>", "<td  style=\"padding:6px 10px; line-height: 150%;border: 1px solid #eee; width: 100px; word-wrap: break-word;\">");
//        htmlText = htmlText.replaceAll("<tr>", "<tr style=\"border-bottom: 1px solid #eee; color:#666;\">");
        try {
            OutputStream out = response.getOutputStream();
            byte[] tag_byte = htmlText.getBytes();
            out.write(tag_byte);
            out.close();
        } catch (Exception e) {
            System.out.println(e);
        }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值