struts2文件图片流输出报IllegalStateException: Cannot call sendError() after the response has been committed

来源:互联网 发布:批处理自动卸载软件 编辑:程序博客网 时间:2024/05/21 19:48

struts2图片后端以流的形式传给前端并显示时,控制台报java.lang.IllegalStateException: Cannot call sendError() after the response has been committed异常.
文件可以正常下载,只是后台报异常.
未修改前代码如下:

 try{              VO vo = new VO();              String filepath = Param.getValue("LOCALACTIVITY", "FILEPATH")+id+postfix;              System.out.println("filepath"+filepath);              File file = new File(filepath);              DataOutputStream temps = new DataOutputStream(response.getOutputStream());;              DataInputStream in = null;              response.setContentType("image/*");              if (file.exists() ) {                  in = new DataInputStream(new FileInputStream(file));                    int len=0;                    byte[] b = new byte[2048];                    while ((len=in.read(b)) != -1) {                        temps.write(b,0,len);                        temps.flush();                    }                    in.close();                    temps.close();              }              else{                    file = new File(Param.getValue("LOCALACTIVITY", "FILEPATH")+"\\default\\default.png");                                      in = new DataInputStream(new FileInputStream(file));                        int len=0;                    byte[] b = new byte[2048];                    while ((len=in.read(b)) != -1) {                        temps.write(b,0,len);                        temps.flush();                    }                    in.close();                    temps.close();              }          }           catch (Exception e) {            log.debug("===========查询图片时报错"+e);        }          log.debug("===========查询图片结束");          return SUCCESS;

研究发现可能是struts2本身的bug,将return SUCCESS 改为return null,即可解决问题.
需要注意的地方:
1.在各分支内部关闭输出流,不要统一关闭.

阅读全文
0 0