zoukankan      html  css  js  c++  java
  • java poi 导出excel弹出下载框报错getOutputStream() has already been called for this response 分享| 2015-01-05 16:30 139****9232 | 浏览 1047 次

    java.lang.IllegalStateException: getOutputStream() has already been called for this response

    2015-01-05 16:30 139****9232 | 浏览 1047 次
    BufferedInputStream in = null;
    BufferedOutputStream out = null;
    if (printFile == null) {
    throw new WebException("传入参数printFile文件为空!");
    }
    try {
    in = new BufferedInputStream(new FileInputStream(printFile
    .getPath()));
    if (printFile.getName().substring(
    printFile.getName().lastIndexOf(".")).equals(".xls")) {

    response.setContentType("application/vnd.ms-excel");
    filename = new String(filename
    .getBytes("GBK"), "ISO8859_1").trim()+".xls";
    response.setHeader("Content-Disposition", streamType
    + ";filename=" + filename);
    }
    out = new BufferedOutputStream(response.getOutputStream());
    byte[] buf = new byte[1024];
    while ((in.read(buf)) > 0) {
    out.write(buf);
    }
    out.flush();
    } catch (IOException e) {
    throw new WebException("生成response出错!", e);
    } catch (Exception e) {
    throw new WebException("生成response出错!", e);
    } finally {
    if (in != null) {
    try {
    in.close();
    } catch (IOException ex) {
    System.out.print("ouputStream.close() fail");
    }
    }
    if (out != null) {
    try {
    out.close();
    } catch (IOException ex) {
    System.out.print("baos.close() fail");
    }
    }
    if(printFile!=null){
    printFile.delete();
    }
    }
    2015-01-05 19:54 网友采纳
     
    我想知道这个方法执行完以后,有没有进行请求转发,这个错误一般是伴随在请求转发时发生的。
    ---------------------------------------------------------------------------------------------------------------------
    以下是我总结的有关Response对象字节流与请求转发之间的异常说明。只供参考!

    情况1: 获得响应对象的字节流response.getOutputStream(),但是不进行任何的操作,也不调用其close()方法,

    此时,在进行请求转发时,会转发成功。
    但是在转发到的jsp页面时,会获得其response的字符流,此时因为response已经获得了其字节流,所以会出现以下异常:
    getOutputStream() has already been called for this response!

    情况2: 获得响应对象的字节流response.getOutputStream(),但是不进行任何的操作,但是调用了该字节流的close()方法,
    此时,在进行请求转发时,不会成功。 因为请求转发器会判断response的字节流是否已提交,如果已提交,则会出现以下异常:
    Cannot forward after response has been committed!

    结论:在整个请求过程中,如果需要进行请求转发,则不能够获得响应对象的字节流!response.getOutputStream()
  • 相关阅读:
    C# .NET 微信开发-------当微信服务器推送消息时如何接收处理
    P1991 无线通讯网[MST]
    P2330 [SCOI2005]繁忙的都市【MST】
    P1546 最短网络 Agri-Net【MST】
    P3225 [HNOI2012]矿场搭建【割点 + 求点双 + 简单组合数】
    P3119 [USACO15JAN]Grass Cownoisseur G [ Tarjan + 缩点 + 拓扑序 + dp + 最长路] [好题]
    P2746 [USACO5.3]校园网Network of Schools [tarjan缩点]
    P1196 [NOI2002]银河英雄传说 【带权并查集】
    P1197 [JSOI2008]星球大战 [删边求连通块个数]
    GPLT L2-004 这是二叉搜索树吗?
  • 原文地址:https://www.cnblogs.com/k-iss/p/5705151.html
Copyright © 2011-2022 走看看