无法调用getWriter(),因为已经调用了getOutputStream()。

5

我有一个使用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>

感谢您的任何帮助。祝好。 问候, 塞巴斯蒂安

1
不是能够复现问题的最短代码。 - Raedwald
请添加堆栈跟踪。 - Jens
5
在将文件发送给客户端后转发到另一页时,不能使用return import,而应该使用return null来代替。请注意,写入文件后必须采用此方法。 - M. Deinum
抱歉,我忘记添加堆栈跟踪信息了,但是M. Deinum解决了这个问题。非常感谢! - Sebastian Szwaczyk
1个回答

1

正如@M.Deinum所说,你需要返回null而不是你试图转发的页面名称。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接