mercredi 4 novembre 2020

How to return a view after download a file

My English Hope you can understand me. Hello guys, im developing a web program using Apache Velocity, but i meet a problem, i cannot return view after output a file by OutPutStream, following is my code:

@Get("test")
public String getPdfInfoByTestId(Invocation invocation, @Param("testIds") String testIds) throws Exception {
    HttpServletResponse response = invocation.getResponse();
    if (testIds.length() == 0) {
        invocation.addModel("status", PdfStatusEnum.NOT_FOUND.getType());
        return "admin_pdf_info";
    }
    String[] idArray = testIds.split(",");
    List<Long> idList = string2LongList(testIds);
    List<ContestOneTest> contestOneTestList = contestOneTestService.getByIdList(idList);
    if (contestOneTestList.size() == 0) {
        invocation.addModel("status", PdfStatusEnum.NOT_FOUND.getType());
        //response.sendRedirect("index");
        return "admin_pdf_info";
    }
    List<PdfInfo> pdfInfos = new ArrayList<>(contestOneTestList.size() + 1);
    //填充表格需要的数据
    for (ContestOneTest contestOneTest : contestOneTestList) {
        PdfInfo pdfInfo = new PdfInfo();
        CtsTestUser ctsTestUser = ctsTestUserService.getUserById(contestOneTest.getActorId());
        pdfInfo.setActorName(ctsTestUser.getName());
        pdfInfo.setTestId(contestOneTest.getId());
        pdfInfo.setPaperName(contestOneTest.getPaperName());
        pdfInfo.setPaperId(contestOneTest.getPaperId());
        if (contestOneTest.getStatus() == OneTestStatusEnum.FINISHED.getValue() && StringUtils.isNotBlank(contestOneTest.getPdfUrl())) {
            pdfInfo.setGenStatus("已处理");
            pdfInfo.setRank("无需排队");
            pdfInfo.setPdfUrl(contestOneTest.getPdfUrl());
        } else {
            pdfInfo.setGenStatus("未处理");
            Long rank = JedisAdapter.zRank(RedisKeyUtil.getNewContestPdfGenQueue(),
                        contestOneTest.getId() + "");
            pdfInfo.setRank(String.valueOf(rank));
            pdfInfo.setPdfUrl("暂无");
        }
    }
    XSSFWorkbook workbook;
    String fileName = new StringBuilder().append(contestOneTestList.size())
            .append("位考试的PDF生成进度").toString();
    try {
        //生成表格并作为下载文件输出
        workbook = ExcelUtils.buildWorkbook(pdfInfos);
        invocation.addModel("status", PdfStatusEnum.HAS_GENERATED);
        invocation.addModel("resultCount", contestOneTestList.size());
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        response.setHeader("Content-Type",
                "application/vnd.ms-excel");
        OutputStream out = response.getOutputStream();
        workbook.write(out);
    } catch (Exception e) {
        e.printStackTrace();
        invocation.addModel("status", PdfStatusEnum.ExcelGenFailed.getType());
    }
    return "admin_pdf_info";
}

Is there any way to solve my problem? Very grateful for all answers or suggesstions!




Aucun commentaire:

Enregistrer un commentaire