解决getOutputStream() has already been called for this response

我是在TagSupport中使用了

response.getOutputStream(),导致的这个异常,参考http://gqsunrise.iteye.com/blog/1935758,解决方案如下,添加代码:pageContext.getOut().clear()

        HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
        response.setHeader("Content-disposition", "attachment;filename="+fileName+"."+fileExt);
        File file = new File(fileURI);
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
            ServletOutputStream servletOutputStream = response.getOutputStream();
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(servletOutputStream);
            byte[] buffer = new byte[2048];
            int bytesRead;
            while ((bytesRead=bufferedInputStream.read(buffer,0,buffer.length))!=-1) {
                bufferedOutputStream.write(buffer,0,bytesRead);
            }
            bufferedOutputStream.flush();
            pageContext.getOut().clear();
            if (bufferedInputStream!=null) {
                bufferedInputStream.close();
            }
            if (bufferedOutputStream!=null) {
                bufferedOutputStream.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

 

posted @ 2014-03-22 17:42  rldts  阅读(11449)  评论(0编辑  收藏  举报