问题描述
我的英语希望您能理解我。 大家好,我正在使用Apache VeLocity开发Web程序,但是遇到一个问题,OutPutStream输出文件后无法返回视图,以下是我的代码:
@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";
}
有什么办法可以解决我的问题? 非常感谢您的所有回答或建议!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)