Seb*_*zyk 5 java spring jsp servlets spring-mvc
我有一个使用 spring mvc 的程序。我编写了两个控制器,第一个用于导入数据,第二个用于生成报告。我在生成控制器时遇到问题。当用户单击生成按钮时,我想生成报告,将报告保存在服务器硬盘上并将报告发送给用户。当我尝试将报告保存在硬盘上时,我遇到了 Illegal state 异常:Cannot call getWriter(), getOutputStream()已经调用。我搜索了解决方案,但找不到匹配的答案。这是我的发电机控制器代码:
\n\n@RequestMapping(value = "/generate", method = RequestMethod.POST)\npublic String generateReport(\n Model model,\n @Valid @ModelAttribute("reportProperties") ReportProperties reportProperties,\n BindingResult result, HttpServletResponse response) {\n if (result.hasErrors()) {\n model.addAttribute("logMessage",\n "Generowanie Raportu nie powiodlo sie.");\n return "import";\n }\n\n //Walidacja dat. Mozna przeniesc na validator\n if(reportProperties.getEndDate().compareTo(reportProperties.getStartDate()) < 0){\n model.addAttribute("logMessage", "Data ko\xc5\x84cowa jest wcze\xc5\x9bniejsza od poprzedniej");\n return "import";\n }\n\n XSSFWorkbook report = null;\n if (reportProperties.getReportType().equalsIgnoreCase("tv")) {\n report = tvReportGenerator.generate(reportProperties);\n } else if (reportProperties.getReportType().equalsIgnoreCase("prod")) {\n report = prodReportGenerator.generate(reportProperties);\n } else {\n report = totalReportGenerator.generate(reportProperties);\n }\n if (report != null) {\n saveReportOnHardDrive(report);\n sendReportToUser(report, response);\n } else {\n model.addAttribute("logMessage",\n "Generowanie Raportu nie powiodlo sie.");\n }\n return "import";\n}\n\nprivate void saveReportOnHardDrive(XSSFWorkbook report) {\n try {\n Resource resource = new ClassPathResource("/general.properties");\n Properties props = PropertiesLoaderUtils.loadProperties(resource);\n String path = props.getProperty("saveFilePath");\n FileOutputStream out = new FileOutputStream(new File(path\n + new Date() + ".xlsx"));\n report.write(out);\n out.close();\n } catch (FileNotFoundException e1) {\n // TODO Auto-generated catch block\n e1.printStackTrace();\n } catch (IOException e1) {\n // TODO Auto-generated catch block\n e1.printStackTrace();\n }\n}\n\nprivate void sendReportToUser(XSSFWorkbook report,\n HttpServletResponse response) {\n try {\n response.setContentType("application/xlsx");\n response.setHeader("Content-Disposition",\n "attachment; filename=generate.xlsx");\n report.write(response.getOutputStream());\n response.flushBuffer();\n } catch (IOException e) {\n // TODO Auto-generated catch block\n e.printStackTrace();\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我尝试了一些关闭和刷新响应 OutputStream 的解决方案,但它不起作用。\n这是我的 import.jsp 文件:
\n\n<body>\n\n<div id="Container">\n\n<h1>Mediaplany GigaShopping <a href="/GigaShopping/resources/szablony/Instrukcja.pdf" target="_blank">instrukcja</a></h1>\n\n<h2>Import Mediaplanu <a href="/GigaShopping/resources/szablony/MediaplanSzablon.xlsx">pobierz szablon</a></h2>\n\n<form method="POST" enctype="multipart/form-data"\n action="/GigaShopping/importMediaplan">\n <input type="file" name="mediaplanFile"/>\n <input type="submit" value="Prze\xc5\x9blij plik"/>\n</form>\n\n<h2>Import cennika <a href="/GigaShopping/resources/szablony/CennikSzablon.xlsx" >pobierz szablon</a><a href="/GigaShopping/pricelist" style="margin-right: 4px;">aktualny cennik</a></h2>\n\n<form method="POST" enctype="multipart/form-data"\n action="/GigaShopping/importPriceList"> \n <input type="file" name="pricelistFile">\n <input type="submit" value="Prze\xc5\x9blij plik">\n</form>\n\n<h2>Generowanie raport\xc3\xb3w</h2>\n\n<form:form method="POST" action="/GigaShopping/generate" commandName="reportProperties">\n <table>\n <tr>\n <td>Typ raportu:</td>\n <td>\n <label><form:radiobutton path="reportType" value="tv"/> M/S TV</label>\n <label><form:radiobutton path="reportType" value="prod"/> M/S PROD</label>\n <label><form:radiobutton path="reportType" value="total"/> M/S TOTAL</label>\n </td>\n </tr>\n <tr>\n <td>Stacja</td>\n <td>\n <form:select path="tvName">\n <form:options items="${televisionsList}"/>\n </form:select>\n </td>\n </tr>\n <tr>\n <td>Od</td>\n <td><form:input type="date" path="startDate" id="startDatePicker"/></td>\n </tr> \n <tr>\n <td>Do</td>\n <td><form:input type="date" path="endDate" id="endDatePicker"/></td>\n </tr>\n <tr>\n <td colspan="2"><input type="submit" value="Generuj"></td>\n </tr>\n </table>\n</form:form>\n\n<form:form method="POST" action="/GigaShopping/requestDBContent" commandName="requestProperties">\n <form:input type="date" id="requestDatePicker" path="date"/>\n <form:select path="tvName">\n <form:option value="wszystkie">--wszystkie--</form:option>\n <form:options items="${televisionsList}"/>\n </form:select> \n <input value="zobacz mediaplan" type="submit" name="requestMediaplanButton" />\n <input value="zobacz zam\xc3\xb3wienia" type="submit" name="requestOrdersButton"/> \n</form:form>\n\n<span class="logMessage">${logMessage}</span>\n\n<footer>\n <a href="http://cns.com.pl">CNS 2015</a>\n</footer>\n\n</div>\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n感谢您的帮助。\n此致,\nSebatian
\n| 归档时间: |
|
| 查看次数: |
8906 次 |
| 最近记录: |