我有一个使用spring mvc的程序。我写了两个控制器,第一个用于导入数据,第二个用于生成报告。我有生成控制器的问题。当用户点击生成按钮时我想生成报告,在服务器硬盘上保存报告并向用户发送报告。当我尝试在硬盘上保存报告时,我遇到了非法状态异常:无法调用getWriter(),getOutputStream()已经调用。我搜索了解决方案,但我找不到匹配的答案。这是我的发电机控制器代码:
@RequestMapping(value = "/generate", method = RequestMethod.POST)
public String generateReport(
Model model,
@Valid @ModelAttribute("reportProperties") ReportProperties reportProperties,
BindingResult result, HttpServletResponse response) {
if (result.hasErrors()) {
model.addAttribute("logMessage",
"Generowanie Raportu nie powiodlo sie.");
return "import";
}
//Walidacja dat. Mozna przeniesc na validator
if(reportProperties.getEndDate().compareTo(reportProperties.getStartDate()) < 0){
model.addAttribute("logMessage", "Data końcowa jest wcześniejsza od poprzedniej");
return "import";
}
XSSFWorkbook report = null;
if (reportProperties.getReportType().equalsIgnoreCase("tv")) {
report = tvReportGenerator.generate(reportProperties);
} else if (reportProperties.getReportType().equalsIgnoreCase("prod")) {
report = prodReportGenerator.generate(reportProperties);
} else {
report = totalReportGenerator.generate(reportProperties);
}
if (report != null) {
saveReportOnHardDrive(report);
sendReportToUser(report, response);
} else {
model.addAttribute("logMessage",
"Generowanie Raportu nie powiodlo sie.");
}
return "import";
}
private void saveReportOnHardDrive(XSSFWorkbook report) {
try {
Resource resource = new ClassPathResource("/general.properties");
Properties props = PropertiesLoaderUtils.loadProperties(resource);
String path = props.getProperty("saveFilePath");
FileOutputStream out = new FileOutputStream(new File(path
+ new Date() + ".xlsx"));
report.write(out);
out.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
private void sendReportToUser(XSSFWorkbook report,
HttpServletResponse response) {
try {
response.setContentType("application/xlsx");
response.setHeader("Content-Disposition",
"attachment; filename=generate.xlsx");
report.write(response.getOutputStream());
response.flushBuffer();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我尝试了一些关闭和刷新响应OutputStream的解决方案,但它没有用。 这是我的import.jsp文件:
<body>
<div id="Container">
<h1>Mediaplany GigaShopping <a href="/GigaShopping/resources/szablony/Instrukcja.pdf" target="_blank">instrukcja</a></h1>
<h2>Import Mediaplanu <a href="/GigaShopping/resources/szablony/MediaplanSzablon.xlsx">pobierz szablon</a></h2>
<form method="POST" enctype="multipart/form-data"
action="/GigaShopping/importMediaplan">
<input type="file" name="mediaplanFile"/>
<input type="submit" value="Prześlij plik"/>
</form>
<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>
<form method="POST" enctype="multipart/form-data"
action="/GigaShopping/importPriceList">
<input type="file" name="pricelistFile">
<input type="submit" value="Prześlij plik">
</form>
<h2>Generowanie raportów</h2>
<form:form method="POST" action="/GigaShopping/generate" commandName="reportProperties">
<table>
<tr>
<td>Typ raportu:</td>
<td>
<label><form:radiobutton path="reportType" value="tv"/> M/S TV</label>
<label><form:radiobutton path="reportType" value="prod"/> M/S PROD</label>
<label><form:radiobutton path="reportType" value="total"/> M/S TOTAL</label>
</td>
</tr>
<tr>
<td>Stacja</td>
<td>
<form:select path="tvName">
<form:options items="${televisionsList}"/>
</form:select>
</td>
</tr>
<tr>
<td>Od</td>
<td><form:input type="date" path="startDate" id="startDatePicker"/></td>
</tr>
<tr>
<td>Do</td>
<td><form:input type="date" path="endDate" id="endDatePicker"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Generuj"></td>
</tr>
</table>
</form:form>
<form:form method="POST" action="/GigaShopping/requestDBContent" commandName="requestProperties">
<form:input type="date" id="requestDatePicker" path="date"/>
<form:select path="tvName">
<form:option value="wszystkie">--wszystkie--</form:option>
<form:options items="${televisionsList}"/>
</form:select>
<input value="zobacz mediaplan" type="submit" name="requestMediaplanButton" />
<input value="zobacz zamówienia" type="submit" name="requestOrdersButton"/>
</form:form>
<span class="logMessage">${logMessage}</span>
<footer>
<a href="http://cns.com.pl">CNS 2015</a>
</footer>
</div>
感谢您的帮助。 问候, Sebatian
答案 0 :(得分:0)
作为@M。迪林说道。
您必须返回 null
,而不是您要转发的网页名称。