当前位置: 代码迷 >> 综合 >> java层将outputstream输出到jsp报错:getOutputStream() has already been called for this response
  详细解决方案

java层将outputstream输出到jsp报错:getOutputStream() has already been called for this response

热度:78   发布时间:2023-12-18 11:05:42.0

注意,我这里的报错情况是输出流 outputstrea是在java层生成的,controller中通过response.getOutputStream(); 获取jsp输出流,再输出到jsp页面,下载文件。

如果下载文件的代码是在jsp页面的,请参考其他百度结果。


Controller:

public OutputStream downloadXLSTargetFile(HttpServletResponse response,String fileName){File f = null;if(fileName != null && !"".equals(fileName)){f = new File(fileName);}//导出文件try {response.reset();response.setContentType("application/x-msdownload");response.setHeader("Content-Disposition","attachment; filename=" + f.getName());OutputStream out = response.getOutputStream();return out;} catch (Exception e) {e.printStackTrace();}return null;}


上面方法返回jsp输出流,将需要下载的文件流赋值给上面这个out变量,然后就返回jsp页面

@Re
  相关解决方案