解决getWriter() has already been called for this response异常


    @RequestMapping(value = "/expcode", method = RequestMethod.POST)
    @ResponseBody
    public void expCode(@RequestParam("numRange") String numRange,
                                 HttpServletRequest request, HttpServletResponse response) throws IOException {
  
        PrintWriter writer  = response.getWriter();
       
            if (numRange==null) {
                writer.write("号段为空");
                writer.flush();
                return;
            }

        //导出excel
        StationCode expStartStationCode = stationCodeList.get(0);
        StationCode expendStationCode = stationCodeList.get(stationCodeList.size() - 1);
        String fileDir = request.getSession().getServletContext().getRealPath("");
        String tempFilePath = "download_new/" + System.currentTimeMillis() + ".xlsx";
        PoiExportUtil.exportFile(400, expList, fileDir + tempFilePath);
        writer.write(tempFilePath + "|" + DateUtils.formatDate(new Date()) + "-共" + (expList.size() - 1) + "条-" + deviceTypeName + "(" + expStartStationCode.getSid()
                + "-" + expendStationCode.getSid() + ").xlsx");
    }

根本原因:在Controller接口方法中,既手动调用PrintWriter向客户端输出内容,又设置了方法返回值。导致servlet需要两次将结果通过PrintWriter输出到客户端,结果报错。像上面这样写就好啦。

你可能感兴趣的:(io,springMvc)